Skip to content

Instantly share code, notes, and snippets.

@emadshaaban92
Created February 6, 2014 18:43
Show Gist options
  • Save emadshaaban92/8850128 to your computer and use it in GitHub Desktop.
Save emadshaaban92/8850128 to your computer and use it in GitHub Desktop.
NUMBER = 600851475143
iter numbers = Iter.init(2, function(int elm){ elm + 1 }, function(int elm){ elm > 0 })
function iter remove_multiples(int a, iter seq){
Iter.filter(function(int elm){ mod(elm, a) != 0 }, seq)
}
function primes_init(iter seq){
match(seq.next()){
case {none} : { next : function () { none } }
case {some:it} : { next : function () { some((it.f1, primes_init(remove_multiples(it.f1, it.f2)))) } }
}
}
iter primes = primes_init(numbers)
function largest_factor(int n, iter ps){
match(ps.next()){
case {none} : { none }
case {some:p} : {
if(p.f1==n) some(p.f1)
else match(mod(n, p.f1)){
case 0 : largest_factor(n/p.f1, ps)
default : largest_factor(n , p.f2)
}
}
}
}
option number = largest_factor(NUMBER,primes)
jlog(Int.to_string(Option.get(number)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment