Advertisement
CrazyDiver

2nd Powers of 2

Dec 12th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.44 KB | None | 0 0
  1. procedure ReadNum(var ch: longint);
  2. begin
  3.   read(ch);  //read num
  4. end;
  5.  
  6. function findPwr(inp: longint): longint;
  7. var
  8.   i, num: longint;
  9. begin
  10.   num := 1;  //num eq 1
  11.   for i := 1 to inp do  //repeat cycle inpuuted value times
  12.     num := num * 2;  //multiplie num on 2
  13.   findPwr := num;  //assign to the function value num value
  14. end;
  15.  
  16. function findMaxPwr(inp: longint): longint;
  17. var
  18.   i, n: longint;
  19. begin
  20.   i := 1;  //i eq 1
  21.   n := 0;  //n eq 0
  22.   while i < inp do  //repeat cycle while i is less then inputted value
  23.     begin
  24.       i := i * 2;  //multiplie i on 2
  25.       n += 1;  //add to n 1
  26.     end;
  27.   findMaxPwr := n;  //assign to the function result n value
  28. end;
  29.  
  30. procedure WritePrevPow(inp: longint);
  31. var
  32.   i: longint;
  33.   checker: boolean;
  34. begin
  35.   checker := true;  //firstly checker value is 1
  36.   for i := -findMaxPwr(inp) to -1 do  //iterate all nums from -(power of inputted num) to -1
  37.     begin
  38.       if(i mod 2 = 0) then  //rum cycle if i is even
  39.         if(findPwr(-i) <= inp) then  //run cycle if power of -i eq or less then inputted num
  40.           begin
  41.             write(' ', findPwr(-i));  //write space and power of -i
  42.             checker := false;  //assign to the checker val 0
  43.           end;
  44.     end;
  45.   if checker then  //if checker eq 1 then
  46.     write('0');  //write '0'
  47. end;
  48.  
  49. var
  50.   inp: longint;
  51.  
  52. begin
  53.   readnum(inp);  //read input num
  54.   WritePrevPow(inp);  //write Previous Powers of inputted num
  55. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement