473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Outputting Prime Numbers

Hey All,
I need a program to input a number, test if it is a prime number or not and then output it if it is prime or output the next prime number if it is not.
So far I have to input section, the function and the prime number test so it works if it is prime but I can't figure out how to output the next prime number.
Any help would be much appreciated.
Apr 4 '07 #1
11 5057
ilikepython
844 Expert 512MB
Hello,
for the next prime number can't you just loop the prime number testing function from the non-prime until it finds a prime number and then return that. If that doesn't work, how does your function work then?
Apr 4 '07 #2
I wasn't sure how to loop a function. Do I just put it in a for loop like any other value?
Apr 4 '07 #3
hi friend
i red you question .
let a be the number is not a prime ...
then we go for a looping statement form a to a+100...
i am sure that there must be a prime in between that range....
Apr 4 '07 #4
hi friend
i red you question .
let a be the number is not a prime ...
then we go for a looping statement form a to a+100...
i am sure that there must be a prime in between that range....

I think you need not to go on checking upto next 100 numbers to find the next prime of the number which is already been found not to be a prime.
its enough to try something like this:

Expand|Select|Wrap|Line Numbers
  1.  read the value of a;
  2. do{
  3.  if a is not a prime
  4.  i)increment a by 1(ie.)a++ );
  5.  ii)check whether a++ is prime or not
  6.     a.)if a++ is prime, print the result and get out of the loop
  7.     b.)else continue the process
  8. }while(a is not a prime)
  9.  
Apr 4 '07 #5
JosAH
11,448 Expert 8TB
hi friend
i red you question .
let a be the number is not a prime ...
then we go for a looping statement form a to a+100...
i am sure that there must be a prime in between that range....
*ahem* The following two numbers are both prime numbers and are more than
100 apart (they are the smallest two primes having this property)

370261 370373

kind regards,

Jos
Apr 4 '07 #6
Thanks for all your help,
I have tried doing the do...while loop and the output comes up with nothing and I can't understand why.
I use an if..else statement to check if it is prime at first and output it if it is. This all works. Then from the else part I did this loop...

Expand|Select|Wrap|Line Numbers
  1. else
  2.        do
  3.        n++;
  4.        while (n != prime);
  5.  
  6.        cout << n << endl;
  7.  
Any ideas why this wouldn't work?
Apr 5 '07 #7
Ganon11
3,652 Expert 2GB
You have forgotten the brackets around your loop. You need

Expand|Select|Wrap|Line Numbers
  1. do {
  2.    n++;
  3. } while (n != prime);
Also, shouldn't you be using your prime() function? This looks like you are comparing n with another number.
Apr 5 '07 #8
Oh I did have brackets at one point but I keep changing it! I change to function() to prime

Expand|Select|Wrap|Line Numbers
  1. bool prime;
  2.    prime = isprime (n);
  3.    if (prime)
  4.    cout << n << endl;
  5.    do
  6.    { 
  7.         n++;
  8.    } while (!prime);  
  9.    cout << n << endl;   
So its actually like this
But with brackets it still doesn't work and I don't understand why.
Apr 5 '07 #9
Ganon11
3,652 Expert 2GB
You should probably use a while loop rather than a do...while loop, and you should have the loop under an else clause instead of after your if statement.
Apr 5 '07 #10
So you mean something like this...

Expand|Select|Wrap|Line Numbers
  1. bool prime;
  2.    prime = isprime (n);
  3.    if (prime)
  4.    cout << n << endl;
  5.    else
  6.    while (!prime)
  7.    {
  8.          n++;
  9.    }  
  10.    cout << n << endl;   
Because this does the same thing, outputs nothing.
Apr 5 '07 #11
JosAH
11,448 Expert 8TB
So you mean something like this...

Expand|Select|Wrap|Line Numbers
  1. bool prime;
  2.    prime = isprime (n);
  3.    if (prime)
  4.    cout << n << endl;
  5.    else
  6.    while (!prime)
  7.    {
  8.          n++;
  9.    }  
  10.    cout << n << endl;   
Because this does the same thing, outputs nothing.
If variable 'prime' was false to start with it stays false in your code because
you never attempt to change it anywhere in your loop.

I'd simply do this:
Expand|Select|Wrap|Line Numbers
  1. for (; !isPrime(n); n++)
  2.    ;
  3. cout << n << endl;
kind regards,

Jos
Apr 5 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.