Start New Discussion within our Software Development Community
program Q2 (Input,Output);


  X,Y,T1,T2 : Integer;
  Flag1,Flag2,Flag3 : Boolean;
  SquareRoot,CubeRoot,Z,D1,D2 : Real;


  X := 1;

  Flag1 := True;
  Flag2 := False;
  Flag3 := False;

  while Flag1 do
      Y := X-1;
      SquareRoot := Sqrt(Y);
      T1 := Trunc(SquareRoot);
      D1 := SquareRoot - T1;

      if(D1>0) then
         Flag2 := False;
      Z := X+1;
      CubeRoot := Exp((1/3) * Ln(Z));
      T2 := Trunc(CubeRoot);
      D2 := CubeRoot - T2;

      if(D2>0) then
         Flag2 := False;
      if ((Flag2) and (Flag3)) then
         Flag1 := False;
         Write('Value = ',X,' ',D1,' ',D2);
         X := X + 1;


I'm trying to find way to make cube root in Turbo PASCAL but when i use CubeRoot := Exp((1/3) * Ln(Z)); keeps giving me runtime errors this is the question for the homework

It is said that there is only one natural number n such that n-1 is a square and n+1 is a cube that is n-1 = square x and n+1 = cube y for natural numbers x and y. find n in PASCAL

Well the answer would be more evident if you debugged it.

You tell me what the values of each part of that line works out to be, so Z, ln(Z), etc.. you tell me which bit fails.. and what the values are.

Are you really using turbo pascal from the 80s? or do you mean the turbo delphi products?

Yes, gotta learn how to debug as well.

Yes the compiler i used is Turbo Pascal unfortunately i checked if the CubeRoot & SquareRoot are computing correctly so i tested them turns out

Ex when input X = 4

The result:

SquareRoot := Sqrt(X);  { Turn out correct  2.0000000000E+00 }
T1 := Trunc(X); { also correct 2 }
D1 := SquareRoot - T1; { is correct 0.0000000000E+00 }

{but with the cube is a different story }
CubeRoot := Exp( V * Ln(Z) ); { Turn out correct 1.5874010520E+00 }
T2 := Trunc(X); { also correct }
D2 := CubeRoot - T2; { is not correct 5.8740105200E-00 it should come out
{ 0.58740105200E+00 }

I also try to use the same program with Dev Pascal its seem to have the same problem with D1 :( it would seem that i cant calculate both of them in the same place

Forgive my denseness (its past 11, and Im about to go to bed but..)

5.8740105200E-00 = 0.58740105200E+00

as 5.8740105200E surely is 0.587... just as 0.587E+00 is also 0.587....

I discovered that u have to seperate the two operation of square root and cube root and it worked and i changed the compiler thanks for the help :twisted:

This question has already been answered. Start a new discussion instead.