Dear Friends,

I am looking a small thing , I need a simple code to check the input whether it's prime , if not can enter same

repeateddly upto get a proper value . Please provide a simple code snippet and help me

Thanks,

Anes

logicslab
15
Unverified User

Dear Friends,

I am looking a small thing , I need a simple code to check the input whether it's prime , if not can enter same

repeateddly upto get a proper value . Please provide a simple code snippet and help me

Thanks,

Anes

Jump to PostHello,

Your going to have to test to see if the number is in a list of the prime numbers. They are fixed after all and you can find out what they are in multiple places on the internet.

For example here is a site that has the first 1000 …

Jump to Postlots of code snippets can be found on the net to check if a given input is prime, here's a few from a quick search

http://holmezideas.com/programming/optimization-techniques/fastest-algorithm-to-check-if-a-given-number-is-prime/http://www.programmingsimplified.com/c/source-code/c-program-for-prime-number

rch1231
169
Posting Shark

Hello,

Your going to have to test to see if the number is in a list of the prime numbers. They are fixed after all and you can find out what they are in multiple places on the internet.

For example here is a site that has the first 1000 primes:

zeroliken
79
Nearly a Posting Virtuoso

lots of code snippets can be found on the net to check if a given input is prime, here's a few from a quick search

http://holmezideas.com/programming/optimization-techniques/fastest-algorithm-to-check-if-a-given-number-is-prime/

http://www.programmingsimplified.com/c/source-code/c-program-for-prime-number

Edited
by zeroliken

logicslab
15
Unverified User

Dear friends,

I am not looking the logic of prime number , i need to ask the system to enter prime number upto i give that .... I admit it include checking of prime with repeated input entry .... please help with a solution

Thanks,

anes

Schol-R-LEA
1,438
Commie Mutant Traitor
Featured Poster

Could you please clarify what it is you need for us? It sounds as if you are having difficulty with the input itself. Is that correct?

Assuming that this is the case, then there are a few options you can choose. The simplest of these is to use `scanf()`

to read in the number, and checking the return value to make sure that it did in fact read in integer (`scanf()`

returns the number of characters read, which in this case would be the number of actual digits read in).

However, using `scanf()`

directly has its disadvantages; for one, it leaves behind whatever extra characters were in the data stream, such as the newline, which you then have to manually clear. I would recommend instead that you read the data in using `fgets()`

, and then using `sscanf()`

(note the extra 's') to extract the integer from the read line of data.

```
int possible_prime, retval;
char buffer[MAX_LINE]; // whatever you set MAX_LINE to be
// ... now we skip to where you're reading in the data...
do
{
printf("Enter a number to test for primality: ");
fgets(buffer, MAX_LINE, stdin);
retval = sscanf(buffer, "%d", &possible_prime); // note the ampersand - we use a pointer to the variable here
} while (retval == 0);
```

Edited
by Schol-R-LEA

logicslab
15
Unverified User

Dear pals,

I create thread on bytes forum , regarding this issue . Please look this thread : Click Here

and help me to implement the RSA algorithm in C. I got the concept of fgets() and sscanf() already . issue in the implementation of primality test for that .please advise

Thanks,

Anes

Schol-R-LEA
1,438
Commie Mutant Traitor
Featured Poster

vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague

If the number is large, use the Miller-Rabin primality test.

logicslab
15
Unverified User

DEAR @SCHOL,

IN RSA ALGORITHM IT(e - public key) MUST BE USE PRIME NUMBER WHICH IS NOT THE CONFACTOR OF 'PHI'

Here we input (or randomly insert) two prime numbers p and q

n = p*q
phi = (p-1)*(q-1)

e = must be a prime number - which we chcek now in this thread - but not cofactor of phi

eg : if we give p = 11 & q = 3

then phi = 10*2 = 20

so we cannot give 5 for e , because it's cofactor of 'phi'(ie 20).

So the value e is a subset of PRIME Numbers .

We need to find the 'd' - Private key from formula

d*e mod phi = 1

Then if M is the Message to encrypt( Here M is integer value in range : 0<M<n)

C- Cipher text , which generate from M

C = M^e mod n[Encryption]

and

M = C^d mod n [Decryption]

This is the criteria(Condition) for RSA algorithm

Dear @vegas,

I am looking your 'Miller-Robin primality test'

Thnks,

Anes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.