Skip to content

Instantly share code, notes, and snippets.

@Shreyes2010
Created January 4, 2012 11:52

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 4, 2012.
    20 changes: 20 additions & 0 deletions Problem_14.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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
    }
    })