Created
January 4, 2012 11:52
-
-
Save Shreyes2010/1559727 to your computer and use it in GitHub Desktop.
Solution to problem 14 in Euler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shreyes <- function(temp) ## Cute function that returns the number of iterations that were preformed. | |
{ c <- 0 | |
while(temp > 1) | |
{ if(temp%%2==0) temp <- temp/2 else temp <- 3*temp + 1 | |
c <- c+1 | |
} | |
return(c) | |
} | |
largest <- 0 | |
num <- 0 | |
system.time(for(i in c(1:1000000)) | |
{ | |
iter <- shreyes(i) # Here we get the number of iterations for "i" and we do it for each number from 1 to 1 million | |
if(iter > largest) # If the number of iterations were greater than the previous largest number of iterations | |
{ # update the largest number of iterations and store the number in "num" | |
largest <- iter | |
num <- i | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment