Statistics

Problem Statement for "PseudoPrimeTest"

Problem Statement

A famous probabilistic primality test makes use of Fermat's Little Theorem from number theory. The theorem states
         p-1
	b     %   p  =  1
for all primes p, with b satisfying 1 < b < p, and % denoting modulus or remainder. To refresh your memory, a prime is an integer greater than 1 whose only factors are 1 and itself. In order to test some potential prime q we do the following:
  • Choose some b-value and compute b^(q-1) % q.
  • If this value is not 1 then you know q is not prime.
  • If this value is 1, then you are more sure q is prime than before.
Given q you will try each b-value beginning with 2 until you reach q-1. If at least one of the b-values fails the test stated above (do not produce 1) then return the lowest failing b-value. If all pass return q.

When computing b^(q-1) % q the numbers can get enormous unless certain measures are taken. For one thing, after each multiplication you can apply the modulus. For example,
	190^11 % 300  =  ((190^5 % 300) * (190^6 % 300)) % 300  .
In addition, repeated squaring can speed up the exponentiation process. For example,
	a^9 = a*a*a*a*a*a*a*a*a      requires 8 multiplications but
        a^9 = a*((a^2)^2)^2          requires 4 multiplications.
We can combine the two methods above as illustrated in the following example where we compute a^11 % 12:
	a^11 % 12 = (a * (a^10 % 12)) % 12 
        a^10 % 12 = (a^5 % 12)^2 % 12 
        a^5  % 12 = (a * (a^4 % 12)) % 12 
        a^4  % 12 = (a^2 % 12)^2 % 12 
	a^2  % 12 = (a*a) % 12  

Definition

Class:
PseudoPrimeTest
Method:
firstFail
Parameters:
int
Returns:
int
Method signature:
int firstFail(int q)
(be sure your method is public)

Constraints

  • q will be between 3 and 32000 inclusive.

Examples

  1. 3

    Returns: 3

    Since 2^2 % 3 = 4 % 3 = 1 simply return 3.

  2. 1729

    Returns: 7

    Here we have 2^1728 % 1729 = 1 3^1728 % 1729 = 1 4^1728 % 1729 = 1 5^1728 % 1729 = 1 6^1728 % 1729 = 1 7^1728 % 1729 = 742 so 7 is returned.

  3. 5

    Returns: 5

  4. 561

    Returns: 3

  5. 7

    Returns: 7

  6. 341

    Returns: 3

  7. 561

    Returns: 3

  8. 645

    Returns: 3

  9. 1105

    Returns: 5

  10. 1387

    Returns: 3

  11. 4

    Returns: 2

  12. 1905

    Returns: 3

  13. 2047

    Returns: 3

  14. 2465

    Returns: 5

  15. 2701

    Returns: 5

  16. 2821

    Returns: 7

  17. 3277

    Returns: 3

  18. 4033

    Returns: 3

  19. 4369

    Returns: 3

  20. 4371

    Returns: 3

  21. 4681

    Returns: 3

  22. 5461

    Returns: 3

  23. 6601

    Returns: 7

  24. 7957

    Returns: 3

  25. 8321

    Returns: 3

  26. 8481

    Returns: 3

  27. 8911

    Returns: 7

  28. 10261

    Returns: 3

  29. 10585

    Returns: 5

  30. 11305

    Returns: 3

  31. 12801

    Returns: 3

  32. 13741

    Returns: 3

  33. 13747

    Returns: 3

  34. 13981

    Returns: 3

  35. 14491

    Returns: 3

  36. 15709

    Returns: 3

  37. 15841

    Returns: 7

  38. 16705

    Returns: 3

  39. 18705

    Returns: 3

  40. 18721

    Returns: 5

  41. 19951

    Returns: 3

  42. 23001

    Returns: 3

  43. 23377

    Returns: 3

  44. 25761

    Returns: 3

  45. 29341

    Returns: 13

  46. 30121

    Returns: 3

  47. 30889

    Returns: 3

  48. 31417

    Returns: 3

  49. 31609

    Returns: 3

  50. 31621

    Returns: 5

  51. 31859

    Returns: 31859

  52. 31873

    Returns: 31873

  53. 31883

    Returns: 31883

  54. 31891

    Returns: 31891

  55. 31907

    Returns: 31907

  56. 31957

    Returns: 31957

  57. 31963

    Returns: 31963

  58. 31973

    Returns: 31973

  59. 31981

    Returns: 31981

  60. 31991

    Returns: 31991

  61. 31992

    Returns: 2

  62. 32000

    Returns: 2

  63. 11

    Returns: 11

  64. 12

    Returns: 2

  65. 13

    Returns: 13

  66. 14

    Returns: 2

  67. 15

    Returns: 2

  68. 16

    Returns: 2

  69. 17

    Returns: 17

  70. 18

    Returns: 2

  71. 19

    Returns: 19

  72. 31859

    Returns: 31859

  73. 32000

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: