Solved

# Nearest prime number

Posted on 1999-12-29
Medium Priority
1,983 Views
How to write a C program to find a prime number nearest to an input interger num.
0
Question by:wewe
• 2

LVL 10

Expert Comment

ID: 2314186
Pls give more info. How many digits, what kind of algorithm to you want, is it a homework?
0

LVL 2

Accepted Solution

yairy earned 40 total points
ID: 2314216
The gap between to prime numbers
near the number N is in average Log*N.

It meen you should just take from the number one more, one less, two more, two less and so on and check
(n+-1, n+-2, n+-3 ...)

Very fast You will find a prime !

How to check if its a prime ?

check all numbers from 2..root(n)
if noone divides it - its a prime.

bool IsPrime(int n)
{
int i;
bool IsAPrime=true;
for (i=2; i<=sqrt(n); i++)
{
if (n%i==0)
IsAPrime=false;
}

return IsAPrime;
}

Yair
0

LVL 2

Expert Comment

ID: 2314217
>> to gap between TWO primes...
0

Author Comment

ID: 2314308
To:rbr
Yes!It is my homework.
I have to write 2 function:
"isPrime" ~ find prime number,if the num is not a prime then return 0; else return 1.
"findPrime" ~ accept an interger and return a prime number which is nearest to it or the same as the given num.
The findPrime function should call the isPrime function to find the prime number.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month5 days, 18 hours left to enroll