How to resolve the algorithm Square but not cube step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Square but not cube step by step in the Modula-2 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 Modula-2 programming language

Source code in the modula-2 programming language

MODULE SquareNotCube;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;

CONST   
    Amount = 30;
VAR     
    CubeRoot, SquareRoot, 
    Cube, Square,
    Seen: CARDINAL;

BEGIN
    Seen := 0;
    SquareRoot := 1;
    CubeRoot := 1;
    Square := 1;
    Cube := 1;
    
    REPEAT
        SquareRoot := SquareRoot + 1;
        Square := SquareRoot * SquareRoot;
        WHILE Square > Cube DO
            CubeRoot := CubeRoot + 1;
            Cube := CubeRoot * CubeRoot * CubeRoot;
        END;
        IF Square # Cube THEN
            Seen := Seen + 1;
            WriteCard(Square, 4);
            WriteLn();
        END;
    UNTIL Seen = Amount
END SquareNotCube.


  

You may also check:How to resolve the algorithm Sum of squares step by step in the PHP programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Lasso programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the Action! programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the CoffeeScript programming language