less_retarded_wiki/pascal.md
2024-11-13 01:37:31 +01:00

943 B

Pascal

TODO

Here is our standardized divisor tree program in Pascal:

program divisorTree;
uses crt;

{ recursive function, prints divisor tree of x }
procedure printDivisorTree(x: integer);
var
  a: integer;
  b: integer;
  i: integer;
begin
  a := -1;
  b := -1;

  for i := 2 to x div 2 do { find two closest divisors }
  begin
    if x mod i = 0 then
    begin
      a := i;
      b := x div i;
 
      if b <= a then
        break;
    end;
  end;

  write('(');

  if a > 1 then
  begin
    printDivisorTree(a);
    write(' ',x,' ');
    printDivisorTree(b);
  end
  else
    write(x);

  write(')');
end;

var
  number: integer;
  code: integer;
  userInput: string[16];

begin
  while true do { main loop, read numbers from the user }
  begin
    readLn(userInput);
    val(userInput,number,code);

    if code <> 1 then
    begin
      printDivisorTree(number);
      writeLn('');
    end
    else
      break;
  end;
end.