-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit. Various versions of a prime number generator.
- Loading branch information
1 parent
f02c5ee
commit 31f8d86
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//Finding all of the prime numbers between 1 and n. | ||
//First crack at it. very inefficient. | ||
|
||
#include <iostream> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
bool isPrime(int num) { | ||
bool prime = true; | ||
if (num < 2) | ||
return false; | ||
for (int i = 2; i < num; ++i) { | ||
if (num % i == 0) | ||
prime = false; | ||
} | ||
return prime; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
for (int j = 0; j <= TOP; ++j) { | ||
if (isPrime(j)) | ||
std::cout << j << " "; | ||
} | ||
std::cout << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
//Finding all of the prime numbers between 1 and n. | ||
//First crack at it. very inefficient. | ||
|
||
#include <iostream> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
bool isPrime(int num) { | ||
bool prime = true; | ||
if (num < 2) | ||
return false; | ||
for (int i = 2; i < num; ++i) { | ||
if (num % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
for (int j = 0; j <= TOP; ++j) { | ||
if (isPrime(j)) | ||
std::cout << j << " "; | ||
} | ||
std::cout << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
bool isPrime(int num) { | ||
bool prime = true; | ||
if (num < 2) | ||
return false; | ||
//reduce the range of numbers being checked to half of the num. | ||
for (int i = 2; i < (num/2)+1; ++i) { | ||
//std::cout << num << " / " << i << " = " << num % i << std::endl; | ||
if (num % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
for (int j = 0; j <= TOP; ++j) { | ||
if (isPrime(j)) | ||
std::cout << j << " "; | ||
} | ||
std::cout << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
|
||
using namespace std; | ||
|
||
bool isPrime(int num) { | ||
bool prime = true; | ||
if (num < 2) | ||
return false; | ||
double maxDivisor = pow((double)num+1, 0.5); | ||
for (int i = 2; i <= (int)maxDivisor; ++i) { | ||
if (num % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
for (int j = 0; j <= TOP; ++j) { | ||
if (isPrime(j)) | ||
std::cout << j << " "; | ||
} | ||
std::cout << std::endl; | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <iostream> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
|
||
bool* sieve = new bool[TOP+1]; | ||
std::fill_n(sieve, TOP+1, 1); | ||
sieve[0] = 0; | ||
sieve[1] = 0; | ||
for (int i = 2; i <= TOP; ++i) { | ||
if (sieve[i] == 1) { | ||
std::cout << i << " "; | ||
for (int j = i; j <= TOP; j+=i) { | ||
sieve[j] = 0; | ||
} | ||
} | ||
} | ||
delete[] sieve; | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argv[1] == NULL) | ||
return 0; | ||
const int TOP = atoi(argv[1]); | ||
std::cout << "Prime Numbers from 0 - " << TOP << ": "; | ||
|
||
const int newTop = ((TOP/2)-1+TOP%2); | ||
|
||
//cout << newTop << endl; | ||
bool* sieve = new bool[newTop+1]; | ||
|
||
std::fill_n(sieve, newTop+1, 1); | ||
|
||
for (int i = 2; i <= (int)pow(newTop, 0.5); ++i) { | ||
if (sieve[i] == 0) { | ||
std::cout << i << " "; | ||
for (int j = i; j <= TOP; j+=i) { | ||
sieve[j] = 0; | ||
} | ||
} | ||
} | ||
delete[] sieve; | ||
return 0; | ||
} | ||
|
||
|