# Stuck on how to store the highest prime number

This is a discussion on Stuck on how to store the highest prime number within the C Programming forums, part of the General Programming Boards category; Hey all, Basically, I am suppose to compute 10 numbers and find their highest prime number for each of them. ...

1. ## Stuck on how to store the highest prime number

Hey all,

Basically, I am suppose to compute 10 numbers and find their highest prime number for each of them.

I've already have the inputs working, and set up 2 arrays to store inputs and their highest prime number. But I am uncertain on how to find and store their highest prime number. Can anyone give me an idea how to start it off?

Thanks.

2. Welcome to the forum, Sizz!

How large are these numbers - can you hold them in an int array?

Do you have a run-time requirement (maybe 5 or 10 seconds) or no time limit? Trial by division method is easy, but slow. Sieve of Eratosthenes is much faster, and not much harder at all. If the numbers aren't too large, the Sieve of E. would be perfect for this job.

Read up on the Sieve of Eratosthenes on Wikipedia, and see what you think.

No, I don't have a runtime requirement. I think my main problem is, if i input 10 numbers, I need to find all the prime numbers from each input i make. Is there an efficient way of doing this??

Oh and thanks for the suggestion on wikipedia, its quite good .

4. So by "compute" do you mean to "factorise"?
If that's not correct then you'll need to explain yourself better.

5. Regardless of what "compute" means, the method of storing a prime value depends on the magnitude of that prime value. I certainly have no idea what "compute highest prime for a value" means, and won't even speculate.

Virtually all C compilers support a long unsigned type, which is able to represent any integral value between zero and 4294967295. Most modern compilers support a long long unsigned type, which is guaranteed able to represent any integral value between zero and 18446744073709551615. If you need a larger prime value than that, or you have an older compiler that does not support long long types, then you need to find an alternative approach.

6. Ahhhhhhh sorry for the confusion. I meant "input" instead of "compute".

7. Originally Posted by Sizz
No, I don't have a runtime requirement. I think my main problem is, if i input 10 numbers, I need to find all the prime numbers from each input i make. Is there an efficient way of doing this??

Oh and thanks for the suggestion on wikipedia, its quite good .
OK, so your program will be taking input for 10 numbers, and needs to find the largest prime number less than each of those 10 numbers, is that right?

So if the first number is 18, you need the prime number 17 as the answer?

If so, I'd put the 10 input number into a small array, and sort them, in ascending order (small...big). Now you're set to use the Sieve of Eratosthenes, because it finds ALL the prime numbers as it works - and every time it passes the value of inputNumbers[i], you have your answer for the next input number at primeNumber[i-1].

Code:
```//inside the Sieve of E.: