﻿ How to check if 4 prime numbers inputted by the user are sequential?

How to check if 4 prime numbers inputted by the user are sequential?

So I thought I solved this problem, but after a while I realized that all my code does, is make sure that all inputs are prime, and that the first 3 inputs are lesser than the 4th. Or vice versa if it's a prime sequence from greater to lesser, so how do I properly make sure that all my 4 prime numbers are sequential? The higher the prime numbers are, the greater the gap between each prime number grows. So I'm pretty stumped here. Also flag++ is there to check if all 4 inputs are prime. If all 4 are prime, then my function returns TRUE.

``````if(input2 > input1) //This is to know if the sequence is going up or going down, in this case, it's up
while (x <= input4) { //Creates all prime numbers while X is lesser than or equal to input4
while (isprime(x) == 0)
x++;
if (isprime(x)) { //If x is a prime number, we check if it's one of the inputs, if all 4 inputs are consecutive prime integers, then flag will be 4 after the loop, and we'll know that it's a prime sequence
if (x == input1)
flag++;
if (x == input2)
flag++;
if (x == input3)
flag++;
if (x == input4)
flag++;
}
x++;
}
``````

I'm going to just let the function start at input1, and if the next prime number isn't input 2, I'll have it return 0 immediately, and if the next prime is input 2, the process repeats until input4. Thanks for the feedback about Erastothenes Sieve, however, I'm not allowed to use arrays for this, and Eratothenes' Sieve needs it. So I'll just implement what I thought of. To anyone who googles this because they have the same problem, you could do that.

``````y = 0, x = input1; //This
if(input2 > input1) //This is to know if the sequence is going up or going down, in this case, it's up
while (x <= input4) { //Creates all prime numbers while X is lesser than or equal to input4
while (isprime(x) == 0)
x++;
if (isprime(x))
y++;
if (isprime(x)) { //If x is a prime number, we check if it's one of the inputs, if all 4 inputs are consecutive prime integers, then flag will be 4 after the loop, and we'll know that it's a prime sequence
//If y is equal to 1, that means that the prime number found is the next input
if (x == input1 && y == 1) {
flag++;
y = 0;
}
if (x == input2 && y == 1) {
flag++;
y = 0;
}
if (x == input3 && y == 1) {
flag++;
y = 0;
}
if (x == input4 && y == 1) {
flag++;
y = 0;
}
}
x++;
}
``````

You might be interested in Wilson's theorem, which states that

For a candidate prime n, n is prime if and only if ((n-1)! + 1) mod n = 0

While I realize that you requested a solution in C, such a solution is not easy to supply due to the wide diversity of "big number" packages, etc, which might be chosen. However, to demonstrate that an implementation of this theorem is possible (and not difficult, given sufficient language support) I'll point out that in Clojure (Lisp variant for the Java VM) this can be implemented as

``````(defn is-prime [n]
(= (mod (+' (apply *' (range 2 n)) 1) n) 0))
``````

No arrays are required here as the "primeness" of any number can be determined directly without knowing the values of any other primes. It does require computing the factorial of potentially large numbers and thus a "big number" math package may be very helpful, but it does allow direct determination of whether any given number is prime, so by iterating all the numbers from the lowest value input to the highest value input it would allow you to determine if there are any intervening primes.

Best of luck.

How do you eat an elephant? One bite at a time. We need to break this problem down into a series of small steps. And from there, it's easy.

Rather than showing you any code, I instead want to lay out the gist of the algorithm for you. Some comments have suggested that the best way to do this would be using an array to create a sieve.

I'd make the case that an array should be used, but not necessarily to create the sieve. Instead, to handle the values input by the user. However, per your teacher's absurd requirements, we're not allowed an array. That's fine--all it means is that our algorithm is built to handle any quantity of inputs. It's rather short sighted, but oh well.

Here are the steps (and it sounds like some of them are already solved).

1. Gather user input.
2. Sort the user's input from lowest to highest.
3. Starting from the lowest user number, which we already know is prime, increment up by twos (all , checking each value to determine whether it's a prime. If we run into a prime before we run into the next user input, we've got a result.
4. Repeat this process for the 2nd to 3rd numbers, 3rd to 4th, and if you had an array of inputs, continue until you reach the end of the array.

Some functions I'd expect to see in this program:

``````bool isPrime(int value);
int nextPrime(int startingPrime);
``````

So, after sorting the values in order, you're looking at something along the lines of...

``````isPrime(value1) && value2 == nextPrime(value1) && value3 == nextPrime(value2) && value4 == nextPrime(value3)
``````