Created
May 30, 2012 02:32
-
-
Save jedifran/2833109 to your computer and use it in GitHub Desktop.
Revisions
-
jedifran created this gist
May 30, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ ################################################################## # Brute force method for finding all the prime numbers between # nMin and nMax ################################################################## findPrimes <- function(nMin, nMax) { # make sure min number is larger than 1, and break if true if (nMin == 1) stop("\n\n **** Error: Please ensure nMin is larger than 1 (nMin > 1) ****") # make sure nMax isnt too large! if (nMax > 1e+08) stop("\n\n **** Error: nMax too large!!, try nMax < 1e8 ****") # create a test range going from nMin to nMax test.range = c(nMin:nMax) # create iterator it it <- 2 # iterate through test.range looking for multiples of smaller numbers while (it < nMax) { if (it %in% test.range) { #test whether 'it' is in test.range test.range <- sort(c(it, test.range[test.range%%it != 0])) #if yes, then remove multiples amd ensure 'it' is added to updated list. this ensures small primes remain in list. } else { test.range <- test.range[test.range%%it != 0] #if no, then remove large multiples } it <- it + 1 } test.range } ####################################### # Usage ####################################### findPrimes(11,87)