Sum of Multiples of Three and Five
EasyPractice library · +10 XPmathloopsdivisibility
Given a positive integer N, find the sum of all positive integers less than or equal to N that are divisible by 3 or 5.
Input
A single line containing a positive integer N.
Output
A single line containing the sum of all positive integers less than or equal to N that are divisible by 3 or 5.
Constraints
1 <= N <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
10
Output
33
Numbers divisible by 3 or 5 up to 10 are 3, 5, 6, 9, 10. Sum = 3 + 5 + 6 + 9 + 10 = 33.
Example 2
Input
20
Output
98
Numbers divisible by 3 or 5 up to 20 are 3, 5, 6, 9, 10, 12, 15, 18, 20. Sum = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 + 20 = 98.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
10
Example 2 input
20