How to resolve the algorithm Square but not cube step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Square but not cube step by step in the OCaml programming language

Table of Contents

Problem Statement

Show the first   30   positive integers which are squares but not cubes of such integers. Optionally, show also the first   3   positive integers which are both squares and cubes,   and mark them as such.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Square but not cube step by step in the OCaml programming language

Source code in the ocaml programming language

let rec fN n g phi =
  if phi < 31 then
    match compare (n*n) (g*g*g) with
    | -1 -> Printf.printf "%d\n" (n*n); fN (n+1) g (phi+1)
    |  0 -> Printf.printf "%d cube and square\n" (n*n); fN (n+1) (g+1) phi
    |  1 -> fN n (g+1) phi
    | _ -> assert false
;;

fN 1 1 1


  

You may also check:How to resolve the algorithm Arrays step by step in the Ring programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Elena programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Program termination step by step in the Slate programming language
You may also check:How to resolve the algorithm Variables step by step in the PHP programming language