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:
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 ?
Thanks in advance.
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 ?
Thanks in advance.