0

Not Yet Answered # Prime Number program.. with a twist

Ok, so I know lots of people have posted about how to find prime numbers up to a certain number, and I can do that, however If I wanted to output a certain number of prime numbers so that the user inputs say 6, it would out put 2, 3, 5, 7, 11, 13.. not just 2, 3, 5 (up to that number). Any ideas? please help!

Salem 5,138 WaltP 2,905 computerman11 zyruz WaltP 2,905 ANUJ SHROTRIYA WaltP 2,905 Rashakil Fol 978 computerman11 computerman11 WaltP 2,905 ANUJ SHROTRIYA Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

0

Primes upto n

```
for ( i = 1 ; i < n ; i++ ) {
if ( isPrime(i) )
}
```

Number of primes

```
i = 1
while ( x < n ) {
if ( isPrime(i) ) x++
i++
}
```

0

In other words, every time you output a value, add 1 to a counter. When it reaches you 'magic number' exit the loop.

0

I have this so far....It doesnt seem to like me, can anyone help?

```
//Nth Prime Number Search
//This is a program which will display the Nth prime number to the user
#include <iostream>
using namespace std;
void primeTest(int);
int main()
{
system("clear");
cout << "Please give me the _th number that you would like " << endl << "(eg. 5 for the 5th Prime number) or hit -1 to exit" << endl;
int n;
cin >> n;
if(n<=0)
{return 1;}
else
primeTest(n);
return 0;
}
void primeTest(int n)
{
int temp = 2;
for (int counter=1; counter<=n; counter++)
{
cout << "Prime number " << counter << " is " << temp << endl;
counter ++;
for (int divisor=2; divisor <= temp; divisor++)
{
if (divisor % temp ==0)
{
break;
}
}
temp++;
}
}
```

0

you are adding counter twice in your loop, need to remove one.

```
void primeTest(int n)
{
int temp = 2;
for (int counter=1; counter<=n; counter++)
{
cout << "Prime number " << counter << " is " << temp << endl;
// counter ++; need to be comented out or removed.
for (int divisor=2; divisor <= temp; divisor++)
{
if (divisor % temp ==0)
{
break;
}
}
temp++;
}
}
```

0

Instead of this

```
if(n<=0)
{return 1;}
else
primeTest(n);
return 0;
```

do this:

```
if (n > 0)
{
primeTest(n);
}
return 0;
```

It's simple and easy to read.

And:

`for (int counter=1; counter<=n; counter++)`

A *for* loop will not give you **n** primes, it will simply test the values **1** thru **n** for prime. Use a *while* loop instead.

0

well i thnk ths will solve ur prob...................

```
#include<stdio.h>
void prime_no(int n);
void main()
{
int n;
printf("ENTER THE NO. OF PRIME NOS. U WANNA SEE");
scanf("%d",&n);
prime_no(n);
return;
}
void prime_no(n)
{
int i,j,count;
for(i=2,count=1;count<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
break;
}
if(j==i)
{printf("%d\n",i);
count++;
}
}
}
```

0

well i thnk ths will solve ur prob...................

If you are going to do people's homework for them, at least give them *good* code in a *well formatted* style.

Better yet, let *them* write the code and you suggest options so they can learn to do it themselves...

0

You could create a lazy list datatype that only produces values when you request them -- and whose values it produces are the natural numbers. Then run the sieve of Eratosthenes on it. Then peel off the first 6 elements of the list.

Or you could use a vector that you grow whenever you need more elements and use the sieve of eratosthenes on that, or some modification thereof.

An alternative is to exploit the prime number theorem. Out of x numbers, the proportion of primes is ~(x/log(x)), so allocate a vector with max(C,ceil(1.15 * n*(1+log n))) elements if you want n primes, for some constant C that's large enough, and then use the sieve of Eratosthenes.

I guess this works better in a language that's lazy by default.

`nPrimes n = take n (nubBy (\x y -> y `mod` x == 0) [2..])`

0

I keep getting stuck at number 25, where is says prime 25 is 95.....up until then it works great

```
//Prime numbers
//March 26th 2007
//CPSC121-03
//This is a program which will display the Nth prime number to the user
#include <iostream>
using namespace std;
int get_prime(int);
int main()
{
system("clear");
int n;
do
{
cout << "Please give me the _th number that you would like " << endl <<
"(eg. 5 for the 5th Prime number) or hit -1 to exit" << endl;
cin >> n;
system("clear");
if (n == 1)
cout << "Prime number 1 is 2" << endl;
else if(n<1)
return 1;
else if (n>1)
{
get_prime(n);
cout << "Prime number " << n << " is " << get_prime(n) << endl;
}
}
while (n>0);
return 0;
}
int get_prime(int n)
{
int prime = 1;
for (int counter=2; counter<=(n); counter++)
{
prime += 2;
int divisor=2;
while(divisor < prime)
{
if ((prime % divisor != 0))
divisor +=1;
else
{
prime += 2;
divisor +=2;
}
}
}
return (prime);
}
```

*Edited 5 Years Ago by Narue*: Removed personal information

0

What the.... :?:

Wouldn't a simple`getchar()`

suffice?

hell, before i had a for statement only with breaks, but my teacher didnt like it........dont worry bout the int main...im pretty sure it works right

0

hell, before i had a for statement only with breaks, but my teacher didnt like it........dont worry bout the int main...im pretty sure it works right

Being "pretty sure" and actually working right may be 2 different things. Remove that `while`

, it's a very bad practice, and add `getchar()`

instead.

And I think your problem is in this section:

```
else
{
prime += 2;
divisor +=2;
}
```

Look carefully at what happens after this code is executed. Run through your code on paper to see what happens.

0

If you are going to do people's homework for them, at least give them

goodcode in awell formattedstyle.Better yet, let

themwrite the code and you suggest options so they can learn to do it themselves...

ok sir!!!!!!!!!!!!!!

i will remember tht..........

thnk u.........

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...