# QuestionPrime numbers and subtraction problem.

#### rideronix

##### New member
Hello everyone,

I'm currently working on a calculator and in order to challenge myself, I decided not to use any loops (while, for, ...) but only use recursive methods (if ... then ... return else return).

So basically what I'm trying to do is create a function that checks if the 2 numbers I've entered are primes numbers and if so, it subtract number A with number B. If the result is 6 then true, else false. A and B may not be greater than 1,000.

Here goes my code:

C#:
``````        static bool prime_6(int a, int B)/>/>
{
int x;
x = 2; // this number will divide number a and b to check if they are prime numbs.

if (a < 1000 && b < 1000) //setting an exception
{
if (a % x != 0 && x < a) // Checking if number a is a prime number.
{
}
if (b % x != 0 && x < B)/>/> // Checking if number b is a prime number.
{
x++;                   // Adding +1 to x
return is_sexy(a, B)/>/>; // Launching back the function
}

return (a - b == 6); // Once it's done (if number a and b are prime, test if a - b = 6)
}

else
{
return false; // Otherwise false

}

}``````

Now, I know some things I've done wrong: setting the "x=2" at the begining will always set my x to 2 and thus never ending the function but I don't see where to place it... any ideas ? Or improvement ? Or other mistakes ?

#### AceInfinity

##### Member
Here's my prime checking function:
C#:
``````private bool IsPrime(int input)
{
if (input == 2) return true;
else if (input == 1 || input % 2 == 0) return false;

for (int n = 3; n < Math.Sqrt(input); n += 2)
if (input % n == 0 && n != input) { return false; }

return true;
}``````

First check both for not being greater than 1000. Then, check with the function and if both numbers return true from calling it, then subtract the numbers, and check the result for == 6... There's lots of ways the code above can be improved. Why did you choose recursion for this though? A for loop was sufficient enough and probably the better strategy anyways Replies
2
Views
102
Replies
8
Views
985
Replies
5
Views
362
Replies
7
Views
104
Replies
2
Views
73