+ 6

M3A1 Sequence

so in math there's a sequence called the m3a1 sequence which is quite easy to explain, so here it goes. when a number is odd, you multiply it by 3 and add 1 to it. but when it's odd you divide it by 2 you do this until you reach 1 or -1, which you ALWAYS do. your goal is to take input from the user, show the number's m3a1 sequence and also count how many tries it took to reach 1. another extra thing you might like to do is find inputs that cause problems and solve them. may the mass*acceleration be with you

20th Aug 2017, 9:17 AM
Not Found
Not Found - avatar
31 Answers
20th Aug 2017, 10:22 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 13
try (-9) as input
20th Aug 2017, 12:55 PM
niteOwLTwO
niteOwLTwO - avatar
+ 13
@Not Found -9, -26, -13, -38, -19, -56, -28, -14, -7, -20, -10, -5, -14... endless loop before reaching 1 or -1
20th Aug 2017, 1:09 PM
niteOwLTwO
niteOwLTwO - avatar
+ 13
@Not Found i don't think it is a bug
20th Aug 2017, 1:17 PM
niteOwLTwO
niteOwLTwO - avatar
+ 12
@Vari93 excellent alg
20th Aug 2017, 6:32 PM
niteOwLTwO
niteOwLTwO - avatar
+ 11
@sayan : I didn't get the question initially. Now that I get it, I edited my code.
20th Aug 2017, 12:27 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 8
@Not Found : Check my code now. It's modified to take any input.
20th Aug 2017, 12:45 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
vari93 let me give an example: let's start with 7, 7 is odd so we multiply it with 3 and add 1 so we get 22. but there is no stopping. 22 is even so we divide it by 2 . we get 11 and then 34, 17,52,26,13,40,20,10,5,16,8,4,2,1 and we are done, observe the pattern
20th Aug 2017, 11:23 AM
Not Found
Not Found - avatar
20th Aug 2017, 11:24 AM
Not Found
Not Found - avatar
+ 4
thanks Randall Young. it's not a bug it's a feature. just kidding. it's a damned bug. I am starting to work on it to solve it.(after a few minutes) now it should work fine. when it's negative, I do 3n-1 so the effect is mirror of the positive side,(how I think it should be). also made it more efficient
20th Aug 2017, 1:13 PM
Not Found
Not Found - avatar
+ 4
@Randall Young well now it isn't šŸ˜‚šŸ˜ƒ
20th Aug 2017, 1:18 PM
Not Found
Not Found - avatar
20th Aug 2017, 2:10 PM
Vari93
Vari93 - avatar
+ 3
both sayan chandra and krishna's code are correct.
20th Aug 2017, 12:32 PM
Not Found
Not Found - avatar
+ 3
the mass is with me but not the acceleration :) https://code.sololearn.com/WJr2VrenqXQC/?ref=app note: the question says twice odd
20th Aug 2017, 3:21 PM
ysraelcon
ysraelcon - avatar
+ 2
I'm a little confused. I get the part where if a number is odd you multiply it by 3 then add 1. The second part confuses me, is it if that m3a1 result is odd you start dividing it by 2 until you reach 1 or -1?
20th Aug 2017, 9:55 AM
Vari93
Vari93 - avatar
20th Aug 2017, 11:56 AM
sayan chandra
sayan chandra - avatar
+ 2
it works fine now šŸ˜ƒ
20th Aug 2017, 12:56 PM
Not Found
Not Found - avatar
+ 2
@Not found Thank you, I will work on this when I get off work.
20th Aug 2017, 12:59 PM
Vari93
Vari93 - avatar
21st Aug 2017, 3:40 PM
ReimarPB
ReimarPB - avatar
+ 2
oh, someone did it before me. rip.
21st Aug 2017, 3:44 PM
ReimarPB
ReimarPB - avatar