Click here to Skip to main content
15,565,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.

What I have tried:

i don't know what is logic behind this question.please help me
Posted
Updated 5-Oct-22 4:04am
Comments
Patrice T 8-Aug-20 1:57am    
Which part of the problem you don't understand ?

It's pretty simple:
1) Create a function which takes an integer parameter called limit
2) Inside the function:
2.1) Create a variable called sum and set it to zero.
2.2) Run a loop from 0 to limit inclusively.
2.3) Inside the loop:
2.3.1) If the loop variable is a multiple of 3, add it to sum
2.3.2) Otherwise, if it's a multiple of 5, add it to sum
2.4) After the loop, return sum

Hint: you can use the modulus operator to check for multiples of n

Give it a try, it's a pretty simple task!
 
Share this answer
 
Comments
Anurag Gandhi 8-Aug-20 5:35am    
Great!! Someone trying to get their homework done here. But, I like the way you helped. :)
CPallini 13-Oct-20 10:56am    
5.
Themba Lawrence Lekhebotsane 23-Oct-21 9:12am    
do you have the code for solution 1
OriginalGriff 23-Oct-21 9:18am    
Yes.
Why?
Python
def calculate_sum(a, N): 
  
    # Number of multiples 
    m = N / a 
  
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
  
    # sum of multiples 
    ans = a * sum
  
    print("Sum of multiples of ", a, 
          " up to ", N, " = ", ans) 
  
# Driver Code 
calculate_sum(7, 49)
 
Share this answer
 
v2
Comments
CPallini 13-Oct-20 10:55am    
My 5.
That's is clever code, albeit incorrect: you should replace the division operator ('/') with the integral division one ('//').
Moreover it doesn't directly answer the OP question.
Dave Kreskowiak 13-Oct-20 11:43am    
Uhhh, it's not a good idea to do someone's homework for them. The OP learns nothing about problem solving when you do that.
def sum_of_numbers(limit):
total = 0
for i in range(limit):
if i % 3 == 0 or i % 5 == 0:
total = total + i
i = i + 1
return total


userInput = int(input("Enter limiting number: "))
plusOne = userInput + 1
print(sum_of_numbers(plusOne))
 
Share this answer
 
Comments
CHill60 9-Jun-21 4:48am    
I doubt the OP still has this homework assignment nearly a year after it was due, and even if they did it doesn't help them to actually code up Solution 1 - which is all you have tried to do. So you haven't really added anything new to the thread.
This may be your own attempt to produce something for your own homework, so here are some points about your code:
- I tried to run your code. All I got was an error "IndentationError: expected an indented block". Indent your code properly - use the formatting bar or spaces.
- The line i = i + 1 is pointless - it's not part of the loop and the value is never used.
- Why are you adding 1 to the User input - that was not a requirement? Instead that should be happening within the sum_of_numbers function because it is only necessary because you chose to use range(limit) - change that to range(limit + 1) - encapsulate it within the function. Or even better explicitly state the full range and use range(0, limit + 1)
def sum_of_multiples(limit):
sum=[]
for number in range(1, limit):
if number%3 == 0:
sum.append(number)
if number%5 == 0:
sum.append(number)
print(sum)

sum_of_multiples(int(input("enter the limit: ")))
 
Share this answer
 
Comments
CHill60 6-Oct-22 6:15am    
Reasons for my downvote:
- You have added nothing new to the thread - read the comments against previous solutions.
- Your code won't even parse/compile - indentation is required
- Your code is poorly written
if number%3 == 0:           sum.append(number)if number%5 == 0:          sum.append(number)
would be better written as
if number%3 == 0 or number%5 == 0:          sum.append(number)
(I can't get the linefeeds into the comment, but do you see the principle)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900