4
\$\begingroup\$

As many people will undoubtedly recognise, this is a question in a well known series of problems that I won't name ;)

It's been a long time since I've written any F# and I'm not sure I've ever come across any canonical style guidelines so I'd like feedback on all aspects of the code.

let largestPrimeFactor (number:int64) =
 let rec inner n current =
 if (n * n) > current then
 current
 else if current % n <> 0L then
 let nextCandidate = if n = 2L then 3L else n + 2L
 inner nextCandidate current
 else
 inner n (current / n)
 inner 2L number

Usage is simply:

let largestFactor = largestPrimeFactor 999999866000004473L
// 999999937

I'm not really concerned with performance... I know about the various sieve options (Eratosthenes, Atkins) and decided that trial division for a single prime was adequate.

200_success
146k22 gold badges190 silver badges478 bronze badges
asked Dec 9, 2015 at 20:26
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The names of the function and parameters inner n current don't mean anything; I suggest testFactor num factor instead. I'd also swap the two parameters so that the one that varies more (the candidate factor) appears last — this is a convention to facilitate currying.

Switching the order of the cases lets you avoid nesting conditions.

let largestPrimeFactor (number:int64) =
 let rec testFactor num factor =
 if factor * factor > num then
 num
 else if num % factor = 0L then
 testFactor (num / factor) factor
 else if factor = 2L then
 testFactor num 3L
 else 
 testFactor num (factor + 2L)
 testFactor number 2L
answered Dec 9, 2015 at 21:26
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.