Statistics

Problem Statement for "SieveOfEratosthenes"

Problem Statement

The Sieve of Erathosthenes is an ancient method used to find all prime numbers between 2 and a given upper bound maxNum, inclusive. It works like this:

make a list of all numbers between 2 and maxNum, inclusive
for x = 2 to maxNum
   if x is not scratched
      for y = 2 to maxNum/x
         if x*y is not scratched, scratch x*y
      end for
   end if
end for

In this fashion, every composite number in the range will get scratched while every prime number will remain unscratched. Return the last number which gets scratched when the Sieve of Eratosthenes is run with the given maxNum.

Definition

Class:
SieveOfEratosthenes
Method:
lastScratch
Parameters:
int
Returns:
int
Method signature:
int lastScratch(int maxNum)
(be sure your method is public)

Constraints

  • maxNum will be between 4 and 2000000000 (2*109), inclusive.

Examples

  1. 18

    Returns: 15

    When x=2, the numbers 4, 6, 8, 10, 12, 14, 16, and 18 are scratched. When x=3, only 9 and 15 are scratched (other multiples like 6 and 12 were already scratched in a previous step). For x=5,7,11,13 and 17, there are no new scratches. Therefore, the answer is 15.

  2. 5

    Returns: 4

    2, 3 and 5 are all primes, so the only scratched number is 4.

  3. 100

    Returns: 91

  4. 400

    Returns: 361

  5. 2000000000

    Returns: 1999878319

  6. 8

    Returns: 8

  7. 1999999998

    Returns: 1999878319

  8. 1999999254

    Returns: 1999878319

  9. 1999999112

    Returns: 1999878319

  10. 1999999066

    Returns: 1999878319

  11. 1999999008

    Returns: 1999878319

  12. 1999878319

    Returns: 1999878319

  13. 1999878168

    Returns: 1999073521

  14. 1999827445

    Returns: 1999073521

  15. 1999599033

    Returns: 1999073521

  16. 1999169514

    Returns: 1999073521

  17. 1999159633

    Returns: 1999073521

  18. 1999073520

    Returns: 1998626411

  19. 1999072210

    Returns: 1998626411

  20. 1998351843

    Returns: 1998179401

  21. 1998131217

    Returns: 1998089999

  22. 1997833237

    Returns: 1997553587

  23. 1997665189

    Returns: 1997553587

  24. 1997150393

    Returns: 1996927969

  25. 1996816063

    Returns: 1996749221

  26. 1996611676

    Returns: 1996570489

  27. 1996416006

    Returns: 1996212557

  28. 1995872735

    Returns: 1995587359

  29. 1995314573

    Returns: 1994247649

  30. 1111111111

    Returns: 1110955561

  31. 1073741824

    Returns: 1073610467

  32. 1000000000

    Returns: 999634589

  33. 999999999

    Returns: 999634589

  34. 594823321

    Returns: 594628189

  35. 134217728

    Returns: 134165873

  36. 43046721

    Returns: 43046657

  37. 20511149

    Returns: 20457529

  38. 16777216

    Returns: 16777207

  39. 2097152

    Returns: 2093809

  40. 707281

    Returns: 703921

  41. 262144

    Returns: 259081

  42. 32768

    Returns: 32761

  43. 24389

    Returns: 23707

  44. 6561

    Returns: 6557

  45. 4096

    Returns: 4087

  46. 1024

    Returns: 961

  47. 841

    Returns: 841

  48. 512

    Returns: 437

  49. 256

    Returns: 247

  50. 128

    Returns: 121

  51. 81

    Returns: 77

  52. 64

    Returns: 49

  53. 32

    Returns: 25

  54. 29

    Returns: 25

  55. 16

    Returns: 15

  56. 9

    Returns: 9

  57. 1999878318

    Returns: 1999073521

  58. 7

    Returns: 6

  59. 6

    Returns: 6

  60. 4

    Returns: 4

  61. 10

    Returns: 9

  62. 25

    Returns: 25

  63. 48

    Returns: 35

  64. 1987654321

    Returns: 1987643873

  65. 1897231767

    Returns: 1895992849

  66. 1916425729

    Returns: 1916425729

  67. 1999878317

    Returns: 1999073521


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: