Prime numbers

Posts 115 of 19 · Page 1 of 2
Prime numbers
as a test for chapter 2 the task was to make a program that can find the prime numbers between 1 and 100. i tried it and i failed. tried again and succeeded.

i googled if 1 is a prime nr but it isn't.

but under no circumstance i can let it show the nr 1 (yeah i know it is not a prime number but i still want it to be possible).

can someone check if the code is alright or can be shortened?
or to make it possible to make the 1 show up.

#include <iostream>
using namespace std;

int main ()
{
int prime, checker, residu;
bool addprime;
residu = prime%checker;

for (prime = 1;prime <=100; prime++)
{

for (checker = 1; checker <prime; checker++)
{
residu = prime%checker;
if (residu ==0)addprime = false;
if (checker ==1)addprime = true;

}

if (addprime == true) cout << prime<< endl;

}

system ("pause");

return 0;

}
Quote Originally Posted by lalakijilp View Post
as a test for chapter 2 the task was to make a program that can find the prime numbers between 1 and 100. i tried it and i failed. tried again and succeeded.

i googled if 1 is a prime nr but it isn't.

but under no circumstance i can let it show the nr 1 (yeah i know it is not a prime number but i still want it to be possible).

can someone check if the code is alright or can be shortened?
or to make it possible to make the 1 show up.
Close, but not quite. You just need to make a compound if statement

if(residu == 0 && checker != 1)


Place that in ur code.... so that the loop will look like this:
Code:
addprime = true;
for (checker = 1; checker <prime; checker++)
{
residu = prime%checker;
if(residu == 0 && checker != 1)addprime = false;
}
also because this handles both checks you don't need the second if statement so I took the liberty of removing it :P
ty didnt think of it to do it that way.

the only thing now is you need to change prime to 2 in the first for loop because 1 isnt actually a prime nr.
err oops searched wikipedia and found this is something different
Quote Originally Posted by Hell_Demon View Post
err oops searched wikipedia and found this is something different
in nl zijn het priemgetallen heb je dat nog niet gehad dan? had ik in de 1ste al
Quote Originally Posted by lalakijilp View Post
in nl zijn het priemgetallen heb je dat nog niet gehad dan? had ik in de 1ste al
ik heb dat voor het laatst op de basisschool gehad 6 jaar geleden :P

mischien is dit interessant voor je: Sieve of Atkin - Wikipedia, the free encyclopedia
Wow. omg they are speaking german o_O? Ummm confused.... it is german right?
Quote Originally Posted by why06 View Post
Wow. omg they are speaking german o_O? Ummm confused.... it is german right?
yeah in holland we speak german
Dutch.. (Nederlands) they're talking about prime numbers and how last time hell demon had that in 'basic school' was 6 years ago* and then he posted a link to an algorithm. Lala responded that it was very confusing with a lot of 'stuff' going on and was wondering if HD understood any of this.


*ya know.. i'm seriously starting to think I'm the old-fart around here...
Quote Originally Posted by B1ackAnge1 View Post
Dutch.. (Nederlands) they're talking about prime numbers and how last time hell demon had that in 'basic school' was 6 years ago* and then he posted a link to an algorithm. Lala responded that it was very confusing with a lot of 'stuff' going on and was wondering if HD understood any of this.


*ya know.. i'm seriously starting to think I'm the old-fart around here...
Is it just me or does my little flag thingy, not actually show up . Oh well, my guess of dutch was correct then =D.
Yeah no little flag thingie for you. Maybe you need to re-set it? I think I had to.
Quote Originally Posted by B1ackAnge1 View Post
Dutch.. (Nederlands) they're talking about prime numbers and how last time hell demon had that in 'basic school' was 6 years ago* and then he posted a link to an algorithm. Lala responded that it was very confusing with a lot of 'stuff' going on and was wondering if HD understood any of this.


*ya know.. i'm seriously starting to think I'm the old-fart around here...
Ummm... how do I put this... ur fart has taken a little longer to dissipate then most other farts o_O?
Code:
#include <iostream.h>
#include <math.h>

using namespace std;

int main(void){

int primelist[10000]={2,3};  //array to store all prime numbers
int pn=2;   //number of primes in array
int i;        //number to check if prime
int i2;      //index of prime list
bool prime;

for(i=5;i<10000;i+=2){
      for(i2=0,prime=true;primelist[i2]<=sqrt(i);i2++){
              if(i%primelist[i2]==0)prime=false;
      }
      if(prime){
              primelist[pn]=i;
              pn++;
      }
}

for(i=0;i<pn;i++)printf("%d\n",primelist[i]);

return 0;
}

My code running time is about (n/2)*(sqrt(n)) h while his code's running time is n squared
Quote Originally Posted by zhaoyun333 View Post

My code running time is about (n/2)*(sqrt(n)) h while his code's running time is n squared
I'm not sure what you mean by this, but it was a nice idea to only check the odd numbers.
Posts 115 of 19 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?