summaryrefslogtreecommitdiff
path: root/src/options.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/options.cpp')
-rw-r--r--src/options.cpp47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/options.cpp b/src/options.cpp
index 7f66028..d9cd8f1 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -6,12 +6,20 @@
6#include <bu/sio.h> 6#include <bu/sio.h>
7#include <stdlib.h> 7#include <stdlib.h>
8 8
9Options::Options( int argc, char *argv[] ) 9Options::Options( int argc, char *argv[] ) :
10 iScale( 0 ),
11 iRadix( 10 )
10{ 12{
13 addOption( iRadix, 'r', "radix",
14 Bu::String("Set the radix (default: %1)").arg( iRadix ) );
15 addOption( iScale, 's', "scale",
16 Bu::String("Set the scale (default: %1)").arg( iScale ) );
11 addOption( Bu::slot(this, &Options::selfTest), "self-test", 17 addOption( Bu::slot(this, &Options::selfTest), "self-test",
12 "Run a series of tests to ensure everything is working correctly."); 18 "Run a series of tests to ensure everything is working correctly.");
13 addOption( Bu::slot(this, &Options::textPrimes), "text-primes", 19 addOption( Bu::slot(this, &Options::textPrimes), "text-primes",
14 "Generate primes in base 36 that only have digits > 9 in them."); 20 "Generate primes in base 36 that only have digits > 9 in them.");
21 addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime",
22 "Test if the given number is prime. Set radix first.");
15 addHelpOption('h', "help", "This help"); 23 addHelpOption('h', "help", "This help");
16 24
17 parse( argc, argv ); 25 parse( argc, argv );
@@ -75,3 +83,40 @@ int Options::textPrimes( Bu::StringArray )
75 return 0; 83 return 0;
76} 84}
77 85
86int Options::isPrime( Bu::StringArray aArgs )
87{
88 Number tst( 0, iRadix );
89 Number max( 0, iRadix );
90 Number j( 0, iRadix );
91 Number one( 0, iRadix );
92 Number fact( 0, iRadix );
93 one = "1";
94 fact = "2";
95
96 Bu::println("Radix: %1").arg( iRadix );
97
98 for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ )
99 {
100 tst = *i;
101 Bu::println("%1").arg( tst );
102 max = tst / fact;
103 bool bPrime = true;
104 for( j = "2"; j < max; j = j + one )
105 {
106 if( (tst%j).isZero() )
107 {
108 bPrime = false;
109 break;
110 }
111 }
112
113 if( bPrime )
114 {
115 Bu::println("%1").arg( tst );
116 }
117 }
118
119 exit( 0 );
120 return aArgs.getSize();
121}
122