+ 2
For loops
How do I compute all multiples of 3&5 in a range?
4 Answers
+ 2
Python # Calculates sum of multiples ofĀ
# a number upto N
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)
+ 2
Роман ŠŃвов
Nice code š
Please verify code is runnable, it didn't run when I checked it out. There are invalid characters that prevents the code to run. Try viewing from a web browser, those characters will show up.
(Edit)
Alternatively view the code in a code editor, then switch encoding to ANSI. Those characters will also show up then.
+ 2
Python requires formatting of blocks with indentation. Work issue :
https://code.sololearn.com/cl3ThlJYtX6Q/?ref=app
0
I'm lost. Is this python?