Prime Number generator from 2 to 100 in r [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Can someone help me and show me the reason why my prime number generator doesn't work?:
d=0
primeno<- c(2:100)
for(i in primeno)
{
for(j in 1:i)
{
if(j %% i == 0)
d=d+1
}
if(d==2)
print ("Prime")
d=0
}

Only inverts the indexes (j %% i) for (i %% j), the number is the index "i" and divisor is the index "j".
d=0
primeno<- c(2:100)
for(i in primeno)
{
for(j in 1:i)
{
if(i %% j == 0)
d=d+1
}
if(d==2)
cat(i,"- Prime \n")
d=0
}

Now, this works:
d=0
primeno<- c(2:100)
for(i in 2:length(primeno)){
for(j in 1:i){
if(i %% j == 0){
d=d+1
}
}
if(d==2){
print(i)
print ("Prime")
}
d <- 0
}

Related

Prime number generator using for loop in R

Trying to create a for loop in which you input a number and the function tells you whether or not the number is prime or not. I used two examples being n1 <- 100 and n2 <- 101.
Here is my code below:
n1 <- 100
n2 <-101
answer = TRUE
for(i in n1){
if (n1 %% (i-1) == 0){
prime1=FALSE
}else {
prime1=TRUE
}
}
prime1
answer = TRUE
for (i in n2){
if (n2 %% (i-1) == 0){
prime2=FALSE
}else {
prime2=TRUE
}
}
prime2
The problem is that the function will generate the same output for both depending on one difference in the code.
In the first "if" statement, both functions will generate output TRUE if I put in the line (i-1). However, if I instead change the line of code to "n1 %% i == 0" as opposed to "n1 %% (i-1) == 0", both functions generate the output FALSE.
Any pointers? Thanks!
Here's a simple prime checker using a for loop.
With n1 <- 100 :
for(i in seq(2, ceiling(sqrt(n1)))) {
if(n1 %% i == 0){
print(FALSE)
break
}
if(i == ceiling(sqrt(n1)))
print(TRUE)
}
#> [1] FALSE
With n2 <- 101
for(i in seq(2, ceiling(sqrt(n2)))) {
if(n2 %% i == 0){
print(FALSE)
break
}
if(i == ceiling(sqrt(n2)))
print(TRUE)
}
#> [1] TRUE

Create sequence in R

Hi I was wondering if someone knows how to realize this sequence in R?
Consider a sequence with following requirement.
a1=1
an=an-1+3 (If n is a even number)
an=2×an-1-5 (If n is a odd number)
e.g. 1,4,3,6,7,10,15,...
a30=?
Try the following.
It will return the entire sequence, not just the last element.
seq_chih_peng <- function(n){
a <- integer(n)
a[1] <- 1
for(i in seq_along(a)[-1]){
if(i %% 2 == 0){
a[i] <- a[i - 1] + 3
}else{
a[i] <- 2*a[i - 1] - 5
}
}
a
}
seq_chih_peng(30)
Note that I do not include code to check for input errors such as passing n = 0 or a negative number.
If you want to do it recursively, you just have the write the equations in your function as follows:
sequence <- function(n) {
if (n == 1) return(1)
else if (n > 1) {
if (n %% 2 == 1) {
return(2 * sequence(n - 1) - 5)
}else{
return(sequence(n - 1) + 3)
}
}else{
stop("n must be stricly positive")
}
}
sequence(30)
# returns 32770

R: recursive too slow [duplicate]

This question already has answers here:
Why is my recursive function so slow in R?
(7 answers)
Closed 7 years ago.
I have R running function:
f <- function (n) {
if ( n == 1) return (0)
if ( n == 2) return (1)
else return ( f(n-1) + f(n-2))
}
f(50) takes very long time to calculate. f(35) takes approx 30 seconds. Is there any way to make it faster in R?
Edit. Thanks for help! I used this and it gave me f(50) instantly.
> f <- local({
+ memo <- c(1, 1, rep(NA,100))
+ f <- function(x) {
+ if(x == 1) return(0)
+ if(x == 2) return(1)
+ if(x > length(memo))
+ stop("’x’ too big for implementation")
+ if(!is.na(memo[x])) return(memo[x])
+ ans <- f(x-1) + f(x-2)
+ memo[x] <<- ans
+ ans
+ }
+ })
This is a comment problem with recursive functions that can be solved by tail-recursion. Unfortunately, it appears that R does not support tail call optimization. Fortunately, you can always use the iterative solution, which you should be fairly fast since it does not have to allocate stack frames:
fIter <- function(n) {
if ( n < 2 )
n
else {
f <- c(0, 1)
for (i in 2:n) {
t <- f[2]
f[2] <- sum(f)
f[1] <- t
}
f[2]
}
}
fIter(100)'
This program runs in ~0.45 seconds on ideone. I don't know R personally, source of program: http://rosettacode.org/wiki/Fibonacci_numbers#R

Producing the nth largest number from a vector in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to have a user enter a number from 0 to 1 (nth number). Returning the nth largest number within say a vector. So when n=1 it acts as the maximum of the vector outputting the largest number. I am new to coding in r. Any help on improving my code would be grateful let alone getting it to work.
KFUN <- function() {
bob <- c(1,2,3,6)
ANSWER <- readline("Enter k value:")
k <- ANSWER
if(k <= 1 && k >= 0) {
if(0 < k <= .25) {
bob[c(4)]
}
if(.25 < k && k <= .5) {
bob[c(3)]
}
if(.5 < k && k <= .75) {
bob[c(2)]
}
if(.75 < k && k <=1) {
bob[c(1)]
}
}
else {
stop("That is not within 0 to 1!")
}
}
KFUN()
Best
Try 'order' function:
nth_largest= function(vect, n){
vect[rev(order(vect))][n]
}
bob <- c(3,6,1,2)
nth_largest(bob, 1)
[1] 6
nth_largest(bob, 2)
[1] 3
nth_largest(bob, 3)
[1] 2
nth_largest(bob, 4)
[1] 1

Characteristic function for algebraic (linear?) function

I would like to create matrix A[i,j,k] with the following elements:
A[i,j,k] = 0 if k+j-s-i =/= 0
A[i,j,k] = p[s] if k+j-s-i =0 ( p[s] is given vector )
This may be written by characteristic function as p[s]*ð(k+j-s-i) or by Kronecker delta function as p[s]*ð(0,k+j-s-i).
Is there any "build in" function in R which gives that - I mean is there "ð" built in?
Or do I have to wrote it by myself?
I suppose it would be very useful to have built function which returns 1 for f(x)=0 and 0 otherwise, at least for linear f(x)
I'd rewrite this as
A[i,j,k] = p[k+j-i] if that exists, otherwise 0
which could then be implemented as
p <- c(1,2,3,4,5)
pfun <- function(x) {
if (x < 1 | x > length(p)) {
0
} else {
p[x]
}
}
n <- 5
A <- array(0, c(n, n, n))
for (i in 1:n) {
for (j in 1:n) {
for (k in 1:n) {
A[i,j,k] <- pfun(k+j-i)
}
}
}
There may be something more elegant than triply-nested for loops.
As for a the function you ask about, something as simple as
as.numeric(f(x)==0)
would work.

Resources