Loading...
Loading...
A maths club gives a sticker for every prime number a member finds between L and R (both included). A prime is a number greater than 1 whose only divisors are 1 and itself. Print how many stickers are possible.
Write a method static boolean isPrime(int n) that checks divisors only up to the square root of n, and call it for every number in the range.
Example 1
Input
1 10
Output
4
The primes are 2, 3, 5 and 7.
Example 2
Input
14 16
Output
0
14, 15 and 16 are not prime.
Try the problem first. Open one hint at a time.
If n has a divisor bigger than √n, it also has one smaller than √n. So test d from 2 while d * d <= n.
Handle n < 2 first: return false.
Return false as soon as one divisor is found; return true after the loop.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor