Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- n = 0; 0
- n = 1; 1
- n = 2; 3
- n = 3; 6
- n = 4; 10
- n = 5; 15
- *)
- (* method 0 with a for loop *)
- let sumNb0 n =
- let sum = ref 0 in
- for i = 0 to n do
- sum := !sum + i
- done;
- !sum;;
- (* method 1 with a direct compute *)
- let sumNb1 n = n * (n + 1) / 2;;
- (* main *)
- (* input *)
- (* TODO: scanf ... *)
- let n = 5;;
- print_int (sumNb0 n);;
- print_int (sumNb1 n);;
- (* for *)
- (* print_int (sumNb1 i);; *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement