Jump to content

Need help making prime numbers row in {c}

Go to solution Solved by Nineshadow,

You could use the sieve or Eratosthenes to generate primes efficiently. That sounds like what you are trying to do , I think.

The sieve of Eratosthenes works this way :

Given a number , p , it's obvious that any multiple of p won't be prime. So, if we were to mark off the multiples of all numbers smaller than our limit, we would be left only with prime numbers.

 

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

  1. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
  2. Initially, let p equal 2, the smallest prime number.
  3. Starting from 2p, enumerate the multiples of p by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

 

Anyway, a really easy function to get the n-th prime would be :

int nthPrime(int n) {    int x, count;    for(x = 2, count = 0; count < n; ++x) {        if (isPrime(x)) {            ++count;        }    }    return x-1;}

Not too efficient though.

Hello,
I need to make a program in {c} that  would give me prime number for entered number (example is user enter 50. to write back 229)
So, I am stuck when making loop. 
I am tring to define for row[100] to have row[0]=2,row[1]=3 and then I make i=4 and try to make a loop that would for number i devide number i with every single number in the row (becose those I know are prime numbers) and get module (number after 0,not sure how it is said on english), and then if if all of them have module !=0 then I know it is prime number and I want to add it into row.

So is there a way somebody can help me write this line? Thanks alot in advance  :)

Link to comment
Share on other sites

Link to post
Share on other sites

It sounds like you know what the algorithm should do, so why not just go ahead and try to code it? If it doesn't work, show your code and we'll help you fix it.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok,ill give you something I have,but the problem is that I do not know how to code that with row

It sounds like you know what the algorithm should do, so why not just go ahead and try to code it? If it doesn't work, show your code and we'll help you fix it.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so as you can see down bellow I made it look for module from 2 and 3,  and that wont work, I need to replace that part with looking for module in all prime numbers that are in row so far. Can you help me there? Thank you

It sounds like you know what the algorithm should do, so why not just go ahead and try to code it? If it doesn't work, show your code and we'll help you fix it.

#include <stdio.h>
int main ()
{
        int i,numb=4,position,temp,temp1,row[100];
        printf(" enter position (1-100)\n");
        scanf("%d",&position);
        if (position>100||position<0 )
        {
             printf("error,enter position between 1 and 100");       //no loop for another entery needed
             return(0);
         }
          row[0]=2;
          row[1]=3;
          i=2;
         do
         {
                temp=numb%2;                       

                temp1=numb%3;
                if (temp!=0 && temp1!=0)
                {
                    row=numb;
                   i++;
                 }
                numb++;
            }
            while (i<100);
            printf("%d. prime number is %d",position,row[position]);
            return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

first of all, please use code tags, everyone will thank you

 

second, you could try going through your row and checking

more or less what you were doing with 2 and 3 bit with all the numbers in row

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

first of all, please use code tags, everyone will thank you

 

second, you could try going through your row and checking

more or less what you were doing with 2 and 3 bit with all the numbers in row

Did see that forum have code tags,my bad.

yes I want to do that with all numbers in a row,but as row keep growing I cant manually type it, I do not know how to make it do that with all numbers in row.

Link to comment
Share on other sites

Link to post
Share on other sites

You could use the sieve or Eratosthenes to generate primes efficiently. That sounds like what you are trying to do , I think.

The sieve of Eratosthenes works this way :

Given a number , p , it's obvious that any multiple of p won't be prime. So, if we were to mark off the multiples of all numbers smaller than our limit, we would be left only with prime numbers.

 

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

  1. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
  2. Initially, let p equal 2, the smallest prime number.
  3. Starting from 2p, enumerate the multiples of p by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

 

Anyway, a really easy function to get the n-th prime would be :

int nthPrime(int n) {    int x, count;    for(x = 2, count = 0; count < n; ++x) {        if (isPrime(x)) {            ++count;        }    }    return x-1;}

Not too efficient though.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so as you can see down bellow I made it look for module from 2 and 3,  and that wont work, I need to replace that part with looking for module in all prime numbers that are in row so far. Can you help me there? Thank you

 

 

-snip-

 

 

Nineshadow gave you another option for how to tackle the problem but I'll try to help you fix up your code anyway.

 

What you'll want to do is use another loop inside your do while that will check your current number against all the prime numbers already added to row. If you find a divisor, the current number isn't prime. If you don't find a divisor, the current number is prime and you can add it to the next spot in the row.

 

I don't have time to show you code right now so give it a try and if you're still having issues, post your attempt and I'll help you later (don't forget the code tags this time ;) )

Link to comment
Share on other sites

Link to post
Share on other sites

You could use the sieve or Eratosthenes to generate primes efficiently. That sounds like what you are trying to do , I think.

The sieve of Eratosthenes works this way :

Given a number , p , it's obvious that any multiple of p won't be prime. So, if we were to mark off the multiples of all numbers smaller than our limit, we would be left only with prime numbers.

 

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

  1. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
  2. Initially, let p equal 2, the smallest prime number.
  3. Starting from 2p, enumerate the multiples of p by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

 

Anyway, a really easy function to get the n-th prime would be :

int nthPrime(int n) {    int x, count;    for(x = 2, count = 0; count < n; ++x) {        if (isPrime(x)) {            ++count;        }    }    return x-1;}

Not too efficient though.

Thank you,it worked,

I completed what I needed using this method.

 

Nineshadow gave you another option for how to tackle the problem but I'll try to help you fix up your code anyway.

 

What you'll want to do is use another loop inside your do while that will check your current number against all the prime numbers already added to row. If you find a divisor, the current number isn't prime. If you don't find a divisor, the current number is prime and you can add it to the next spot in the row.

 

I don't have time to show you code right now so give it a try and if you're still having issues, post your attempt and I'll help you later (don't forget the code tags this time ;) )

Thank you madknight, yes I see what I need to do there,but I just know how to code that (I am still learning programming,this is basicly my university "homework". I used what nineshadow wrote,however If you have time I would like to see how would you make that work. Thank you :-)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×