Total Prime

Easy
0/40
profile
Contributed by
161 upvotes
Asked in company
LTIMindtree

Problem statement

Write a program to find the total number of a primes number in a given interval.

Given two integers S and E, count all primes between S and E.

Detailed explanation ( Input/output format, Notes, Images )
Input format :
The only line of input contains two integers S and E separated by a single space.
Output format :
The only line of the output prints the total number of primes.
Constraints
1 <= N <= 100
Sample Input 1 :
2 10
Sample Output 1 :
4
Explanation of Sample Input 1:
The prime numbers between 2 and 10
are 2,3,5 and 7
Sample Input 2 :
2 5
Sample Output 2 :
3

.

Approaches (1)
Brute-Force Approach
  • Create an outer loop where we iterate from S to E(inclusive).
  • We create a flag variable whose value is set to True in the beginning.
  • Now in a nested loop(inner loop) we iterate from 2 to i / 2 and check if i is divisible by j , if i becomes divisible by j, then it means i has a factor so it is not a prime and thus set the value of the flag to False and break from this inner loop.
  • At the end of the outer loop, we check if the value of the flag is True increment the value of count denoting the number of primes by 1.
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Total Prime
Full screen
Console