-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_BigInt2.cpp
88 lines (76 loc) · 2.29 KB
/
test_BigInt2.cpp
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
// optional argument: number of digits per number
#include "BigInt2.hpp"
int main(int argc, char **argv){
int nDigits = 1;
if (argc > 1) {
nDigits = stoi(argv[1]);
}
// ask for two arbitrary-size integers n1 and n2 and a “small” integer n
// (n must fit within an int)
// for the subtraction to work, n2 must be no larger than n1
cout << "n1 (BigInt): ";
string n1_;
cin >> n1_;
cout << "n2 (BigInt): ";
string n2_;
cin >> n2_;
cout << "n? (Int): ";
int n;
cin >> n;
// convert the strings n1_ and n2_ to arbitrary-size integers
Bigint n1(n1_, nDigits);
Bigint n2(n2_, nDigits);
// print the sum, difference, product, Euclidean division, and modulo
cout << "n1 + n2 = " << n1+n2 << endl;
cout << "n1 - n2 = " << n1-n2 << endl;
cout << "n1 * n2 = " << n1*n2 << endl;
cout << "n1 // n2 = " << n1/n2 << endl;
cout << "n1 % n2 = " << n1%n2 << endl;
// see if n1 and n2 are prime
if(isPrime(n1)){
cout << n1 << " is prime" << endl;
} else {
cout << n1 << " is not prime" << endl;
}
if(isPrime(n2)){
cout << n2 << " is prime" << endl;
} else {
cout << n2 << " is not prime" << endl;
}
// print n1 to the power n
cout << "n1 ^ n = " << pow(n1, n) << endl;
// print n1!
Bigint facn1 = factorial(n1);
cout << "n1! = " << facn1 << endl;
// random number
random_device rd;
mt19937 gen(rd());
unsigned int nRN;
string maxRN_s;
cout << "Number of random numbers:" << endl;
cin >> nRN;
if (nRN > 0) {
cout << "Maximum number:" << endl;
cin >> maxRN_s;
Bigint maxRN(maxRN_s, nDigits);
uniform_int_distribution<unsigned long> dist(0, 10); //
for (int i=0; i<nRN-1; i++) {
cout << randomBigint(maxRN+1, gen) << ", ";
}
cout << randomBigint(maxRN+1, gen) << endl;
}
// print the first few prime numbers
cout << "Number of primes:" << endl;
unsigned int nPrimes;
cin >> nPrimes;
if (nPrimes > 1) {
cout << "The first " << nPrimes << " primes are:" << endl;
} else if (nPrimes == 1) {
cout << "The first prime is:" << endl;
}
vector<Bigint> primes = firstPrimes(nPrimes);
for(int i=0; i<nPrimes-1; i++){
cout << primes[i] << ", ";
}
cout << primes[nPrimes-1] << endl;
}