0
Why does the multiples code fail at some stages of testing
My code seems to pass some stages of test. I probably have missed something logic here https://code.sololearn.com/cBA29vayD1m7/?ref=app
21 Respuestas
0
Denise thanks for the advice.
+ 4
Gemuh Hans
You need to calculate the sum of all the multiples of 3 or 5 below a given number.
Below means < not <=
In your code you add 10 to the multiples when you enter 10, so your sum is 33 instead of 23.
+ 3
that's my code:
sum = 0
for i in range(1, int(input())):
if i % 3 == 0 or i % 5 == 0:
sum += i
print(sum)
+ 3
Gemuh Hans
I'll put it this way: if you use extremely many and extremely large numbers, it might make a difference. Of course, this also depends on the device.
Above all, you improve the readability of your code if you avoid unnecessary steps.
+ 3
A solution using functional approach (it's more pythonic):
```
print(sum(filter(
lambda x: x % 3 == 0 or x % 5 == 0,
range(int(input()))
)))
```
+ 1
I am trying to get the multiples of 3 numbers and sum them. The sum so take into consideration only one common multiple for both numbers.
The code challenge is Multiples
+ 1
Denise and Lamron
Well guys I think I could optimise my code to be shorter and more user friendly. My logic was what guided my code.
+ 1
Good
0
What are you trying to do? And what's the problem/name of the code coach?
0
Oh, I did that yesterday. Your way of doing it seems to be overcomplicated to me.
Try to use **for** loop, **if** statement a and **%** modulo operator. That's what I did
0
Thank you. What's your way.there are multiples ways of doing the same thing lamron. Any I got it right already with help from another mentor .
0
Thank you Denise. I corrected the sign and it work. That was probably a typo
0
Gemuh Hans that's how I did it:
https://code.sololearn.com/cTpCwfmZ8LIk/?ref=app
0
Gemuh Hans
Lamron is right. You can shorten your code if you don't uses lists.
sum = 0
for n in range(1,integer):
...
print(sum)
0
Lamron let me explain what I did. I tried to create the list of all the multiples of the numbers then I looped through the numbers adding them to a set then I transform the set into a list and sum the numbers
0
Denise Roßberg I think my code can also be shorten?
Because the first **if** statement is looking at multiples of both 3 and 5. The second/third statement will not have to use **and not** operators and another expression — As one of the 3 will be true, and will not allow other statements to execute ?
UPDATE:
Yes, my code can be shorten - made changes
0
Lamron I see your code doesn't actually create the multiples of both numbers but it just creates a larger range of numbers from which you test each one if it's a multiple of both numbers. I like the logic. I think we can both learn a thing from eachother's code
0
Indeed
0
Denise. Lovely your logic is short. Does a longer code like mine impact on the functioning of a program in real life? What I mean is does it consume more CPU and memory?
- 1
Gemuh Hans , I can see, it's an interesting way to me