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

Recommended Answers

All 8 Replies

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:

http://primes.utm.edu/lists/small/1000.txt

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

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);

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

OK, that does explain things a little, but it still isn't clear what you really need. It sounds as if you are looking to test whether a given public key's numerical value is prime; but that makes no sense - the whole point of the encryption algorithm is that it uses values too large to be factored in a reasonable time period given current hardware. That's why you select 'pseudo-primes' (numbers very likely to be prime, but which can't be factored quickly) rather than ensuring the primality for the keys.

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

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 = pq
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, networking, learning, and sharing knowledge.