Last active
August 29, 2015 14:19
-
-
Save aomoriringo/c9507d6c345e4f3c0a00 to your computer and use it in GitHub Desktop.
Project Euler 3
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
" Require: https://github.com/vim-jp/vital.vim | |
let s:big = vital#of('vital').import('Data.BigNum') | |
function! P003() | |
let l:num = s:big.from_string("600851475143") | |
let l:primes = Factorize(l:num) | |
for i in l:primes | |
echo s:big.to_string(i) | |
endfor | |
endfunction | |
function! Factorize(num) | |
let l:primes = [] | |
let l:n = deepcopy(a:num) | |
let l:a = s:big.from_num(2) | |
while 1 | |
while 1 | |
let [l:div, l:mod] = s:big.div_mod(l:n, l:a) | |
if s:big.compare(l:mod, s:big.from_num(0)) == 0 | |
let l:n = l:div | |
call add(primes, l:a) | |
else | |
break | |
endif | |
endwhile | |
if s:big.compare(l:n, s:big.from_num(1)) == 0 | |
break | |
endif | |
let l:a = s:big.add(l:a, s:big.from_num(1)) | |
endwhile | |
return l:primes | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment