You'd want a loop. The loop would go through all possible values of P and test whether A and B fall into the first or second condition for every value of P you go through. You'd start at P = 2, the smallest prime number possible, and work your way up.
Would be helpful to have a function that checks if a given value is a prime number or not, that way you can skip an iteration where your value isn't prime.
Once you have a P that does fit the criteria, that's your smallest P value.
I am, however, not finding this example sensical:
For the input 3 6, the output would be 1 2. For the input 70 84, the output would be 5 3. |
For 3 and 6, the prime number is 3 which divides 3, giving 1, and divides 6, giving 2.
However, 70 and 84 doesn't work. The P value here would be 14, dividing 70 to get 5. But even ignoring the fact that 14 isn't prime, 84 / 14 would be 6, not 3. And 14^2 doesn't divide 84. So I don't know where that example came from - its either wrong or I'm misinterpreting something.