Why does the multiples code fail at some stages of testing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

21st Jan 2023, 11:41 AM
Gemuh Hans
21 Answers
0
Denise thanks for the advice.
21st Jan 2023, 3:19 PM
Gemuh Hans
+ 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.
21st Jan 2023, 2:36 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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)
21st Jan 2023, 2:58 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
21st Jan 2023, 3:17 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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())) ))) ```
23rd Jan 2023, 1:32 AM
Евгений
Евгений - avatar
+ 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
21st Jan 2023, 2:33 PM
Gemuh Hans
+ 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.
21st Jan 2023, 2:49 PM
Gemuh Hans
+ 1
Good
23rd Jan 2023, 10:03 AM
ADWAITH B A
ADWAITH B A - avatar
0
What are you trying to do? And what's the problem/name of the code coach?
21st Jan 2023, 2:30 PM
Lamron
Lamron - avatar
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
21st Jan 2023, 2:36 PM
Lamron
Lamron - avatar
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 .
21st Jan 2023, 2:39 PM
Gemuh Hans
0
Thank you Denise. I corrected the sign and it work. That was probably a typo
21st Jan 2023, 2:40 PM
Gemuh Hans
21st Jan 2023, 2:41 PM
Lamron
Lamron - avatar
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)
21st Jan 2023, 2:42 PM
Denise Roßberg
Denise Roßberg - avatar
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
21st Jan 2023, 2:44 PM
Gemuh Hans
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
21st Jan 2023, 2:45 PM
Lamron
Lamron - avatar
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
21st Jan 2023, 2:54 PM
Gemuh Hans
0
Indeed
21st Jan 2023, 2:55 PM
Lamron
Lamron - avatar
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?
21st Jan 2023, 3:01 PM
Gemuh Hans
- 1
Gemuh Hans , I can see, it's an interesting way to me
21st Jan 2023, 2:46 PM
Lamron
Lamron - avatar