How to resolve the algorithm Sequence of non-squares step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sequence of non-squares step by step in the OCaml programming language
Table of Contents
Problem Statement
Show that the following remarkable formula gives the sequence of non-square natural numbers:
This is sequence A000037 in the OEIS database.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sequence of non-squares step by step in the OCaml programming language
Source code in the ocaml programming language
# let nonsqr n = n + truncate (0.5 +. sqrt (float n));;
val nonsqr : int -> int = <fun>
# (* first 22 values (as a list) has no squares: *)
for i = 1 to 22 do
Printf.printf "%d " (nonsqr i)
done;
print_newline ();;
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
- : unit = ()
# (* The following check shows no squares up to one million: *)
for i = 1 to 1_000_000 do
let j = sqrt (float (nonsqr i)) in
assert (j <> floor j)
done;;
- : unit = ()
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Perl programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the PHP programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Go programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Standard ML programming language