ike ike - 9 months ago 177
R Question

"no function to return from, jumping to top level"

Could someone help me, I do not understand what is the problem here. I just started learning R (a few days ago). Here I have to construct a function that will give me all Marsenne primes after I created Marsenne numbers. There are 3 functions that I created:
1) factorlist simply find all the factors of a number
2) Marsenne number is a function that creates Marsenne numbers
3) CheckForMersennePrimes is a function that checks whether founded Mersenne numbers are prime or not.
***function "primes" gives all prime numers that are in interval from 0 to x

The problem is:
First 2 functions works perfectly. But third one does not work and gives the following below mistake. I cannot understand what is wrong at all. What I tried to do is since Mersenne numbers not all prime, I look into set of MersenneNumber and check every element in it whether it is prime or not and then remove it.

Can someone, please, help to find whats wrong?

Here are my functions:

factorlist<-function(x) {
if (x<2) {return ("Invalid input")}
if (x%%1!=0) {return ("Invalid input")}
if (x==2) {return(2)}
if (x==3) {return(3)}
vec<-0
for(i in 1:x){
if(x%%i==0){
vec[length(vec)]<-i
vec<-c(vec,0)
}
}
vec<-vec[-length(vec)]
vec<-vec[-length(vec)]
return(vec)
}

primes <- function(n){
primesR <- function(p, i = 1){
f <- p %% p[i] == 0 & p != p[i]
if (any(f)){
p <- primesR(p[!f], i+1)
}
p
}
primesR(2:n)
}

MarsenneNumber<-function(x){
vec<-c(0)
for (i in 3:x){
vec<-union(vec, c(2^primes(i)-1))
}
return(vec[-length(1)])
}

CheckForMersennePrimes<-function(x){
b<-length(MarsenneNumber(x)){
for (i in 1:b){
vec<-MarsenneNumber(x){
if (factorlist(vec[i])!=1){
vec<-c(-vec[i])
}
}
}
}
return(vec)
}


My mistake to last function:

Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> return(vec)
Error: no function to return from, jumping to top level
> }
Error: unexpected '}' in "}"


Here is an example of how this functions works.

> factorlist(20)
[1] 1 2 4 5 10

> primes(20)
[1] 2 3 5 7 11 13 17 19

> MarsenneNumber(20)
[1] 3 7 31 127 2047 8191 131071 524287

Answer Source

CheckForMersennePrimes has extra {} on line 2 and line 4 (inside for loop) of this function, like vec<-MarsenneNumber(x){if()...}

I removed those extra braces.

CheckForMersennePrimes <- function( x ){
  b <- length( MarsenneNumber( x ) )
  for ( i in 1:b ){
    vec <- MarsenneNumber( x )
    if ( factorlist( vec[i] ) != 1 ){
      vec <- c( -vec[i] )
    }
  }
  return( vec )
}

I think, your intention is to get values of vec in which factorlist(vec[i]) is equal to 1. If this is true, then you have to initiate vec outside the for loop and check for this condition. Then finally return a1 which contains all vec values passing the if condition.

I also shortened your code by removing b variable. You do not need it. Instead you could do seq_along(vec). It will do the same thing without b variable.

CheckForMersennePrimes <- function( x ){
  vec <- MarsenneNumber( x )
  a1 <- c()
  for ( i in seq_along( vec ) ){
    if ( factorlist( vec[i] ) == 1 ){
      a1 <- c( a1, vec[i ] )
    }
  }
  return( a1 )
}

Further condensing the code will give same output. I removed seq_along and indexing of vec

CheckForMersennePrimes <- function( x ){
  vec <- MarsenneNumber( x )
  a1 <- c()
  for ( i in vec ){
    if ( factorlist( i ) == 1 ) {
      a1 <- c( a1, i )
    }
  }
  return( a1 )
}