CrazyDiver

Cycles/Armstrong nums[I]

Dec 12th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.92 KB | None | 0 0
  1. procedure read2nums(var a, b: longint);
  2. begin
  3.   read(a, b);  //read 2 nums
  4. end;
  5. function findPower(a,b:longint):longint;
  6. var i,res:longint;
  7. begin
  8.   res:=1;  //assign to the result val 1
  9.   for i:=1 to b do  //repeat cycle b times
  10.     res:=res*a;  //result eq itselp multiplie wth a
  11.   findPower:=res;  //assign to the func val result var
  12. end;
  13. function CountNumDschs(ch: longint): longint;
  14. var
  15.   kol: longint;
  16. begin
  17.   kol:=0;  //firstly kol variable eq 0
  18.   while ch > 0 do  //repeat cycle while inputted num more than 0
  19.     begin
  20.       kol := kol + 1;  //add to the kol var 1
  21.       ch := ch div 10;  //cut the last discharge of inputted num
  22.     end;
  23.   CountNumDschs := kol;  //assign the the function result value kol variable
  24. end;
  25.  
  26. function findArmst(inp: longint): longint;
  27. var
  28.   num, sum, copy: longint;
  29. begin
  30.   sum:=0;  //firsly sum eq 0
  31.   copy := inp;  //assign to the copied var inputted val
  32.   while inp > 0 do  //repeat cycle while inputted more than 0
  33.     begin
  34.       num := inp mod 10;  //assign to the num variable last discharge of inputted variable
  35.       sum := sum + findPower(num, CountNumDschs(copy));  //add to the sum value whole part of power num^(count of discharges)
  36.       inp := inp div 10;  //cut the last discharge
  37.     end;
  38.   findArmst := sum;  //assign to the result value sum variable
  39. end;
  40.  
  41. procedure WriteArmsNums(a, b: longint);
  42. var
  43.   ch: longint;
  44.   checker: boolean;
  45. begin
  46.   checker:=false;
  47.   for ch := a to b do  //iterate nums from a to b
  48.     begin
  49.       if findArmst(ch) = ch then  //if num is armstrong then
  50.         begin
  51.           write(ch, ' ');  //write this num
  52.           checker := true;  //checker is true
  53.         end;
  54.     end;
  55.   if not(checker) then  //if checker isnt true then
  56.     write('-1')  //write -1
  57. end;
  58.  
  59. var
  60.   a, b: longint;
  61.  
  62. begin
  63.   read2nums(a, b);  //read borders of the range
  64.   WriteArmsNums(a, b);  //write all Armstrong num in range from a to b
  65. end.
Add Comment
Please, Sign In to add comment