Write a Python program to find the sum of Composite numbers between 1 to N. The value of N to be entered by the user.
Answers
Answered by
2
Explanation:
Answer:
- The idea is to create an array of n elements and initialize them to 0 ( which makes this algorithm O(n) complex with respect to space too ).
- Now for every element index, mark its multiples so that they’re not evaluated. For example our current index is 2 so multiples would be 4,6,8,10...100 . At all these indexed, place a flag ( place value 1 at these indexes ) which will tell that this number is not prime OR it is composite.
- Move on to the next element (index) which happens to be 3 , now mark its multiples too. 6,9,12,...99 .
- If the current index flag is set (value = 1), don't evaluate it, skip to the next. For example : 4 will not be evaluated as it’s marked as 1, move on to the next that is 5 .
Attachments:
Answered by
9
Explanation:
numr=int(input("Enter range:"))
print("Prime numbers:",end=' ')
for n in range(1,numr):
for i in range(2,n):
if(n%i==0):
break
else:
print(n,end=' ')
Hope it will help you Dear.
Similar questions
English,
19 days ago
Business Studies,
19 days ago
English,
1 month ago
Art,
1 month ago
History,
8 months ago