View Single Post
Old Aug 30th, 2006, 9:48 PM   #7
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
Its funny you should bring this up. Earlier today I made a similar program, though mine just goes through all numbers upto 1,000,000,000:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int ind = 0;
  7. int prime;
  8. int max;
  9.  
  10. int list[10000000] = {2};
  11.  
  12. for (int i = 3;i < 1000000000;i++) {
  13.  
  14. prime = 1;
  15.  
  16. for (int j = 0; j < ind; j++) {
  17. if ((i%list[j]) == 0) {
  18. prime = 0;
  19. break;
  20. }
  21. }
  22.  
  23. if (prime == 1) {
  24. ind++;
  25. list[ind] = i;
  26. cout << i << "\n";
  27. }
  28. }
  29. return 0;
  30. }

I had an earlier version that brute forced every number, but I found that only using primes works much faster.
glimmy is offline   Reply With Quote