Last active
August 29, 2015 14:18
-
-
Save aomoriringo/a16ea1b208af69d39ad9 to your computer and use it in GitHub Desktop.
Project Euler 003
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
reltime: 54.894354 |
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
function! P003() | |
let l:start = reltime() | |
let l:num = StringToBigint("600851475143") | |
let l:primes = Factorize(l:num) | |
for i in l:primes | |
echo BigintToString(i) | |
endfor | |
echo reltimestr(reltime(l:start)) | |
endfunction | |
function! Factorize(num) | |
let l:primes = [] | |
let l:n = deepcopy(a:num) | |
let l:a = StringToBigint("2") | |
while 1 | |
while 1 | |
let l:tmp = BigMod(l:n, l:a) | |
if BigCompare(l:tmp, StringToBigint("0")) == 0 | |
let l:n = BigDiv(l:n, l:a) | |
call add(primes, l:a) | |
else | |
break | |
endif | |
endwhile | |
if BigCompare(l:n, StringToBigint("1")) == 0 | |
break | |
endif | |
let l:a = BigAdd(l:a, StringToBigint("1")) | |
endwhile | |
return l:primes | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment