Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to find the sum of all the multiples of 3 or 5 below 1000 in Python

2 Answers

0 votes
result = sum(x for x in range(1000) if x % 3 == 0 or x % 5 == 0)

print(result)



'''
run:

233168

'''

 



answered Apr 14, 2025 by avibootz
0 votes
LIMIT = 999

# Get the upper bounds 
upper_for_three = LIMIT // 3
upper_for_five = LIMIT // 5
upper_for_fifteen = LIMIT // 15

# calculate the sums
sum_three = 3 * upper_for_three * (1 + upper_for_three) / 2
sum_five = 5 * upper_for_five * (1 + upper_for_five) / 2
sum_fifteen = 15 * upper_for_fifteen * (1 + upper_for_fifteen) / 2

# calculate the total sum
total_sum = int(sum_three + sum_five - sum_fifteen)

print(total_sum)



'''
run:

233168

'''

 



answered Apr 14, 2025 by avibootz
...