Prime numbers

Hi guys, I bring a difficult problem this time (at least for me) :D

check this out, I made a calculator to operate with fractions, which also simplifies the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream.h>
#include<conio.h>
#include<math.h>

int main(void)
{
    int N1,D1,N2,D2,NT,DT,Y,P;
    double N3,D3,NT2,DT2;
    char X[2]={'Y','/0'};
    cout<<"Calculadora de fracciones v2.1\nPor Pablo Cassinerio\n\n";
    while(X[0]=='y'||X[0]=='Y'){
    cout<<"1-Suma\n2-Resta\n3-Multiplicacion\n4-Division\n5-Potencia\n\nQue desea hacer? ";
    cin>>Y;
    
    switch(Y){
              case 1:{cout<<"Ingrese el numerador de la fraccion 1: ";
                      cin>>N1;
                      cout<<"Ingrese el denominador de la fraccion 1: ";
                      cin>>D1;
                      cout<<"Ingrese el numerador de la fraccion 2: ";
                      cin>>N2;
                      cout<<"Ingrese el denominador de la fraccion 2: ";
                      cin>>D2;
                      NT=(N1*D2)+(N2*D1);
                      DT=D1*D2;
                      }break;
              case 2:{cout<<"Ingrese el numerador de la fraccion 1: ";
                      cin>>N1;
                      cout<<"Ingrese el denominador de la fraccion 1: ";
                      cin>>D1;
                      cout<<"Ingrese el numerador de la fraccion 2: ";
                      cin>>N2;
                      cout<<"Ingrese el denominador de la fraccion 2: ";
                      cin>>D2;
                      NT=(N1*D2)-(N2*D1);
                      DT=D1*D2;
                      }break;
              case 3:{cout<<"Ingrese el numerador de la fraccion 1: ";
                      cin>>N1;
                      cout<<"Ingrese el denominador de la fraccion 1: ";
                      cin>>D1;
                      cout<<"Ingrese el numerador de la fraccion 2: ";
                      cin>>N2;
                      cout<<"Ingrese el denominador de la fraccion 2: ";
                      cin>>D2;
                      NT=N1*N2;
                      DT=D1*D2;
                      }break;
              case 4:{cout<<"Ingrese el numerador de la fraccion 1: ";
                      cin>>N1;
                      cout<<"Ingrese el denominador de la fraccion 1: ";
                      cin>>D1;
                      cout<<"Ingrese el numerador de la fraccion 2: ";
                      cin>>N2;
                      cout<<"Ingrese el denominador de la fraccion 2: ";
                      cin>>D2;
                      NT=N1*D2;
                      DT=D1*N2;
                      }break;
              case 5:{cout<<"Ingrese el numerador ";
                      cin>>N3;
                      cout<<"Ingrese el denominador ";
                      cin>>D3;
                      cout<<"Ingrese la potencia a elevar ";
                      cin>>P;
                      NT2=pow(N3,P);
                      DT2=pow(D3,P);
                      NT=NT2;
                      DT=DT2;
                      }break;
              }
    while(NT%2==0&&DT%2==0||NT%3==0&&DT%3==0||NT%4==0&&DT%4==0||NT%2==5&&DT%2==5||NT%6==0&&DT%6==0||NT%7==0&&DT%7==0||NT%8==0&&DT%8==0||NT%9==0&&DT%9==0){
    if(NT%2==0&&DT%2==0) {NT=NT/2;
                         DT=DT/2;
                         }
    else if(NT%3==0&&DT%3==0) {NT=NT/3;
                               DT=DT/3;
                               }
    else if(NT%5==0&&DT%5==0) {NT=NT/5;
                               DT=DT/5;
                               }
    else if(NT%7==0&&DT%7==0) {NT=NT/7;
                               DT=DT/7;
                               }
    else {NT=NT;
          DT=DT;
          }
          }
    cout<<"El resultado es "<<NT<<"/"<<DT;
    cout<<"\n\nDesea realizar otra operacion? (Y/N) ";
    cin>>X[0];
}
    if(X[0]!='y'||X[0]!='Y') cout<<"Pulse una tecla para salir del programa";
    getche();
}



the program works perfectly well, but I'm a perfeccionist with my programs, and I found a detail that makes my program imperfect: a problem with simplification.

If the result is 4/8, it will be simplified to 1/2, BUT, if the result is 13/26, it won't simplify. I know it's because the simplifier isn't set to simplify by 13, but if i assigned all prime numbers, my program would be neverending!!

Is there a function or something like that to set the simplifier to use all prime numbers? Because I sincerely have NO idea :D
Google "greatest common divisor" and click on the link for euclid's algorithm.

I wrote a templatized version of the GCD function (euclid's algorithm) a while back:

1
2
3
4
5
6
7
8
9
template< size_t A, size_t B >
struct GCD {
    enum { value = GCD< B, A % B >::value };
};

template< size_t A >
struct GCD<A,0> {
    enum { value = A };
};


It's a very simple recursive function.


Also, you should consider trying to reduce the inputted fractions before performing the math to reduce the possibility of overflow when you multiply.
Last edited on
I see, I will try it. However, can u explain to me exactly how that template works? I'm not familiar with templates yet
A template allows having the same functions, classes etc with user specified types.
See http://www.cplusplus.com/doc/tutorial/templates.html
So the way you use my function is like this:

 
cout << GCD<12,6>::value << endl;


That instantiates the first template with A == 12 and B == 6.
The enum inside that template instance recursively instantiates
GCD<A,B> again, this time with A = 6 and B = 0.

But I've specialized the GCD template for all cases where B == 0, so
this time the second template is instantiated with A == 6. This instantiation
sets value = A, or 6 in this case.

So GCD<12,6>::value == GCD<6,0>::value == 6.

Hint: Note the use of the word "recursively" above. You'll also note
that Euclid's algorithm is recursive.

The webpage I referenced all but gives you the C++ code to implement GCD.

Topic archived. No new replies allowed.