-
-
Save svaksha/fead94bb7f6dc96b14c4 to your computer and use it in GitHub Desktop.
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 characters
#Python/pypy | |
import math | |
def smallestdivisall(n): | |
for i in xrange(1, math.factorial(n)+1): | |
for j in xrange(1, n+1): | |
if i % j != 0: | |
break | |
elif j == n: | |
return i | |
#IPython timer | |
In [1]: %time smallestdivisall(20) | |
#Python w/ Numba @autojit | |
import math | |
from numba import autojit | |
@autojit | |
def smallestdivisall(n): | |
for i in xrange(1, math.factorial(n)+1): | |
for j in xrange(1, n+1): | |
if i % j != 0: | |
break | |
elif j == n: | |
return i | |
#IPython timer | |
In [1]: %time smallestdivisall(20) | |
#Julia, 2nd run to remove compilation time | |
function smallestdivisall(n::Int64) | |
for i = 1:factorial(n) | |
for j = 1:n | |
if i % j !=0 | |
break | |
elseif j == n | |
return i | |
end | |
end | |
end | |
end | |
#Julia time macro | |
julia> @time smallestdivisall(20) | |
#R | |
smallestdivisall <- function(n){ | |
require("iterators") #Need because large values for n don't fit in vector, so use iterator | |
m <- icount(factorial(n)) | |
i <- nextElem(m) | |
while(i < factorial(n)) { | |
for (j in 1:n) { | |
if (i %% j != 0) { | |
break | |
} else if (j == n) { | |
return(i) | |
} | |
} | |
i <- nextElem(m) | |
} | |
} | |
#R timer | |
system.time(smallestdivisall(20)) | |
#Compile function to see speed up | |
library(compiler) | |
enableJIT(1) | |
smallestdivisall_c <- cmpfun(smallestdivisall) | |
#R timer | |
system.time(smallestdivisall_c(20)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment