Advertisement
CrazyDiver

Modified Euclid algo

Dec 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.06 KB | None | 0 0
  1. procedure readNums(var ch1, ch2: longint);
  2. begin
  3.   read(ch1, ch2); //read 2 nums
  4. end;
  5. function FindNOD(var count: longint; in1, in2: longint):longint;
  6. begin
  7.   count := 0;  //firstly count eq 0
  8.   while (in1 <> 0) and (in2 <> 0) do  //repeat cycle while inputted values not eq 0
  9.     begin
  10.       if in1 >= in2 then  //if 1st inputted value more or eq 2nd then
  11.         in1 := in1 mod in2  //assign to the 1st inputted value remainder of division 1st on 2nd
  12.       else  //else
  13.         in2 := in2 mod in1;  //assign to the 2st inputted value remainder of division 2st on 1nd
  14.       count := count + 1;  //add to the counter variable 1
  15.     end;
  16.   findNOD := in1 + in2;  //assign to the function result sum of 1st and 2nd values
  17. end;
  18. procedure writeNod(NOD, count: longint);
  19. begin
  20.   writeln(NOD, ' ', count);  //write NOD, space, count of loops in finding NOD function
  21. end;
  22.  
  23. var
  24.   count, in1, in2, NOD: longint;
  25. begin
  26.   readNums(in1, in2);  //read 2 nums
  27.   writeNod(FindNOD(count,in1,in2),count);  //Write NOD of this 2 nums and count of loops in finding NOD function
  28. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement