Added menu dialog
authorChristoph Fuerst <ch.fuerst@gmx.at>
Sun, 26 Mar 2017 18:22:33 +0000 (20:22 +0200)
committerChristoph Fuerst <ch.fuerst@gmx.at>
Sun, 26 Mar 2017 18:22:33 +0000 (20:22 +0200)
src/primefactors.cpp

index 7de4e33..45a1498 100644 (file)
@@ -100,45 +100,78 @@ void factor_int2(mpz_class N)
        cout << "Found prime-factors: " << (a-b)/2 << " and " << ((a+b-2)/2) << endl;
 }
 
-int main()
+int main(int argc, char** argv)
 {
-   mpz_class N;
-   mpz_class *trialdivisors, *factors;
-   unsigned long max;
-   unsigned long nooffactors;
-
-   cout << "Enter Integer to be factored.... ";
-   cin >> N;
-   cout << endl;
-
-   max = (unsigned long)(sqrt(N.get_ui()));
-
-   trialdivisors = new mpz_class[max];
-   factors = new mpz_class[max];
-
-   trialdivisors[0] = 2;
-   trialdivisors[1] = 3;
-   trialdivisors[2] = 5;
-   for(unsigned int i=3;i<max;i++)
+   if(argc == 1)
    {
-      if(i%2 == 1)
-      {
-         trialdivisors[i] = trialdivisors[i-1]+2;
-      }
-      else
-      {
-         trialdivisors[i] = trialdivisors[i-1]+4;
-      }
+          char input = '0';
+
+          do
+          {
+                  cout << "What do you want to do..." << endl;
+                  cout << "Algorithm A: Trial division" << endl;
+                  cout << "Algorithm C: Fermat like factorization" << endl;
+           cin >> input;
+           cout << endl;
+          }while((input != 'A') && (input != 'C') );
+
+          switch(input)
+          {
+                  case 'A':
+                  {
+                          cout << "Select Trial divison" << endl;
+                          mpz_class N;
+                          mpz_class *trialdivisors, *factors;
+                          unsigned long max;
+                          unsigned long nooffactors;
+
+                          cout << "Enter Integer to be factored.... ";
+                          cin >> N;
+                          cout << endl;
+
+                          max = (unsigned long)(sqrt(N.get_ui()));
+
+                          trialdivisors = new mpz_class[max];
+                          factors = new mpz_class[max];
+
+                          trialdivisors[0] = 2;
+                          trialdivisors[1] = 3;
+                          trialdivisors[2] = 5;
+                          for(unsigned int i=3;i<max;i++)
+                          {
+                             if(i%2 == 1)
+                             {
+                                 trialdivisors[i] = trialdivisors[i-1]+2;
+                             }
+                             else
+                             {
+                                 trialdivisors[i] = trialdivisors[i-1]+4;
+                             }
+                          }
+
+                          factor_int(N, trialdivisors, nooffactors, factors);
+
+                          for(unsigned long i=0;i<=nooffactors;i++)
+                                  cout << factors[i] << " ";
+                          cout << endl;
+                          break;
+                  }
+                  case 'C':
+                  {
+                          cout << "Select Fermat like factorization" << endl;
+                          cout << "ATTENTION: Only works proper if there are exactly 2 (!) prime factors" << endl;
+                          mpz_class N;
+                          cout << "Enter Integer to be factored.... ";
+                          cin >> N;
+                          cout << endl;
+                          factor_int2(N);
+                          break;
+                  }
+                  default:
+                          break;
+          }
    }
 
-   factor_int(N, trialdivisors, nooffactors, factors);
-
-   for(unsigned long i=0;i<=nooffactors;i++)
-          cout << factors[i] << " ";
-   cout << endl;
-
-   factor_int2(N);
-
    return (0);
 }