f# - computing prime factors using same code produces different results? -


i trying compute factors of biginteger prime, have 2 simple factorization functions, both should produce same result in way used them here down below not case here, can explain happening?

let lookuptable = new hashset<int>(primes)  let isprime x = lookuptable.contains x  let factors (n:bigint) =      seq.filter (fun x -> n % x = 0i) [1i..n]  let factors' (n:bigint) =      seq.filter (fun x -> n % x = 0i) [1i..bigint(sqrt(float(n)))]   600851475143i |> fun n -> bigint(sqrt(float(n))) |> factors |> seq.map int |> seq.filter isprime |> seq.max // produces 137  600851475143i |> factors' |> seq.map int |> seq.filter isprime |> seq.max // produces 6857 (the right answer) 

your functions not equivalent. in first function, list of candidates goes n, , filter function uses n remainder calculation. second function, however, uses n remainder calculation, candidates list goes sqrt(n) instead.

to make second function equivalent, need reformulate this:

let factors' (n:bigint) =      let k = bigint(sqrt(float(n)))     seq.filter (fun x -> k % x = 0i) [1i..k] 

update, clarify somewhat:
in above code, notice how k used in 2 places: produce initial list of candidates , calculate remainder within filter function? precisely change made code: code uses k in both places, code uses k in 1 place, n in other.

this how original function k:

let factors' (n:bigint) =      let k = bigint(sqrt(float(n)))     seq.filter (fun x -> n % x = 0i) [1i..k] 

notice how uses k in 1 place, n in other.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -