| | |
Print Prime Numbers(using for loop)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Ah it's just occured to me what Salem meant by square root !
A revised edition:
Now if only I could make i++ step in primes!
A revised edition:
C Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; static const int MAX = 500; static const int MIN = 0; static const int MAXCOL = 10; bool isPrime(int r); int main(int argc, char *argv[]) { int col = 0; int n; for(n=MIN; n<=MAX; n++) if(isPrime(n)){ cout << n << '\t'; col++; if(col == MAXCOL){ cout << '\n'; col = 0; } } system("PAUSE"); return 0; } bool isPrime(int n) { if((n == 0)||(n == 1)) return false; int factors = 2; for(int i=2; i<=((int)sqrt((double)n)); i++){ if(n%i == 0){ factors++; break; } } if(factors == 2) return true; return false; }
Now if only I could make i++ step in primes!
>can anyone better it?
Yes, there are more faster prime number finding Al-Gore-it-hims out there.
http://en.wikipedia.org/wiki/Sieve_of_Atkin
Note I said faster as opposed to efficient. An efficient prime number list finding strategy would be rather elusive.
Also, I'm sure you can think of a much better way to pause the program than using
:lol:
Yes, there are more faster prime number finding Al-Gore-it-hims out there.
http://en.wikipedia.org/wiki/Sieve_of_Atkin
Note I said faster as opposed to efficient. An efficient prime number list finding strategy would be rather elusive.
Also, I'm sure you can think of a much better way to pause the program than using
system("PAUSE");?:lol:
•
•
•
•
Originally Posted by iamthwee
Also, I'm sure you can think of a much better way to pause the program than usingsystem("PAUSE");?
:lol:
system("PAUSE") ?I
•
•
Join Date: Apr 2006
Posts: 26
Reputation:
Solved Threads: 0
I dont know that high math, but I think I can do better.
C Syntax (Toggle Plain Text)
bool isPrime(int n) { bool prime ; int lim; if ( n == 0 || n == 1 || n % 2 == 0) return(false); prime = true; lim = (int)sqrt(n); for(int i=3; i<= lim && prime ; i+=2) prime = n % i; return(prime); }
> for(int i=2; i<=((int)sqrt((double)n)); i++)
And it would be so much quicker if you didn't call sqrt() on every iteration of the loop!
n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against.
Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3.
Also, if you start at 3, then you can do i+=2 to only check all the odd numbers from there on.
And it would be so much quicker if you didn't call sqrt() on every iteration of the loop!
n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against.
Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3.
Also, if you start at 3, then you can do i+=2 to only check all the odd numbers from there on.
Dude543 yes I like that very much.
Salem good feedback.
So system("PAUSE") then, what gives ? I havn't found anything negative on the internet yet.
Ok I've found it:
http://www.gidnetwork.com/b-61.html
Salem good feedback.
So system("PAUSE") then, what gives ? I havn't found anything negative on the internet yet.
Ok I've found it:
http://www.gidnetwork.com/b-61.html
you can't edit a post more than 30 minutes after you posted in this forum.
0 and 1 are not prime. Yes 1 is divisible by 1 and itself, but that's the same thing it's only one factor not two factors so it's not prime.
2 is a special case, it is the only even number that is a prime.
0 and 1 are not prime. Yes 1 is divisible by 1 and itself, but that's the same thing it's only one factor not two factors so it's not prime.
2 is a special case, it is the only even number that is a prime.
![]() |
Similar Threads
- Help with prime numbers (C)
- C++ Finding Prime Numbers (C++)
- (Noob) need some help w/ Prime Numbers (C++)
- prime numbers (C++)
Other Threads in the C Forum
- Previous Thread: game 2d array search help
- Next Thread: Had a homework problem in c programming and need help!!
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






