Advertisement
Benjamin_Loison

Little Python basic stuff

Jul 28th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.43 KB | None | 0 0
  1. (*
  2.     n = 0; 0
  3.     n = 1; 1
  4.     n = 2; 3
  5.     n = 3; 6
  6.     n = 4; 10
  7.     n = 5; 15
  8. *)
  9.  
  10. (* method 0 with a for loop *)
  11.  
  12. let sumNb0 n =
  13.     let sum = ref 0 in
  14.     for i = 0 to n do
  15.         sum := !sum + i
  16.     done;
  17.     !sum;;
  18.  
  19. (* method 1 with a direct compute *)
  20.  
  21. let sumNb1 n = n * (n + 1) / 2;;
  22.  
  23. (* main *)
  24.  
  25. (* input *)
  26.  
  27. (* TODO: scanf ... *)
  28. let n = 5;;
  29. print_int (sumNb0 n);;
  30. print_int (sumNb1 n);;
  31.  
  32. (* for *)
  33. (* print_int (sumNb1 i);; *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement