I'm currently attempting to learn Ocaml, and I'm working thought the Project Euler problems to do so. Here's some code I knocked together for problem 10
(* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. MODIFYING CODE FROM PROBLEM 7 *) (* first thing we are going to do is write a bit of code that checks to see if a number is prime *) let rec isPrimeRec number start = if (start*start)>number then 1 else if number mod start = 0 then -1 else isPrimeRec number (start+1);; let i = ref 2;; let sum = ref 0 in while !i <2000000 do if (isPrimeRec !i 2) = 1 then begin sum:= !sum + !i; i:= !i + 1; Printf.printf "%d is prime, it is prime number %d\n" !i !sum; end else i:= !i + 1 done;; let temp = ref 0;; temp:= isPrimeRec 19 3; Printf.printf "The value is %d\n" !temp
Now - I almost purposesly didn't right this to be efficient algorithmically (for example I am aware that prime numbers can't be even) and there are a few other things that i would change for efficiency - but I'm interested in style feedback - so I'd like the code critiqued much more on the level of "In ocaml, one would normaly bracket expression X for readability" or "Ocaml let's you use this, clearly syntax instead" - rather than "it's a property of prime numbers that"