0

# hi , can you help me ? what's wrong with my coding ? did i make any mistake ? 😭😭😭😭😭

2 Answers

+ 2

Your isprime functions returns 1 for alll even positive integers integers, and 0 for all odd integers > 2 (and has undefined behavior for input <= 1). That is clearly not what you intend. You should return 0 if you found a divisor that is strictly less than num (and larger than one), and return one if after the loop passes without returning. (better - after the loop "return num > 1;" , to correctly show that integers up to 1 are not prime)
Then inside mersenne prime you repeat recalculating b in the loop, so the resulting b value is 2^(a-1)-1 - - you don't need a loop here just calculate b=2^a - 1;
Then instead of checking b==a (which i guess always fails) you should check if b is a prime number. Finally mersenne_prime function is never called.

+ 2

After you fix these errors, you might want to fix less important problems:
a) your isprime function is very suboptimal - you might want to optimize it a bit for testing large Mersenne numbers, or even make a separate function that uses properties of Mersenne numbers to test them (https://en.m.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test)
b) generally it is not a good idea to mix logic and output in one function like your mersenne_prime - it reduces the possibility for testing and reusal. Its better if the function returns for example M_p if M_p is prime and 0 otherwise, and the output is done in main or in a separate function called from main