Skip to content

Instantly share code, notes, and snippets.

@aomoriringo
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aomoriringo/c9507d6c345e4f3c0a00 to your computer and use it in GitHub Desktop.
Save aomoriringo/c9507d6c345e4f3c0a00 to your computer and use it in GitHub Desktop.
Project Euler 3
" 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