Advertisement
Guest User

Untitled

a guest
May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int cmmdc (int a, int b) {
  4.     if (a > b) {
  5.         return cmmdc(a - b, b);
  6.     }
  7.     if (a < b) {
  8.         return cmmdc(a, b - a);
  9.     }
  10.     if (a == b) {
  11.         return a;
  12.     }
  13. }
  14. int toPrime(int a) {
  15.     for (int i = 1; i <= a / 2 ; ++i) {
  16.         if (a % i == 0) {
  17.             if (a / i == 1) {
  18.                 return a;
  19.             }
  20.             else {
  21.                 a = a / i;
  22.             }
  23.         }
  24.     }
  25.     return a;
  26. }
  27. int main() {
  28.     float N, M;
  29.     cin >> N >> M;
  30.     int count = 0, steps;
  31.     while (N != 0 && M != 0) {
  32.         count++;
  33.         if (cmmdc(N, M) != 1) {
  34.             steps = (M - N) / toPrime(M - N);
  35.         }
  36.         else {
  37.             steps = -1;
  38.         }
  39.         cout << "Case " << count << ": " << steps;
  40.         cin >> N >> M;    
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement