+ 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
31 Answers
+ 13
try (-9) as input
+ 13
@Not Found
-9, -26, -13, -38, -19, -56, -28, -14, -7, -20, -10, -5, -14... endless loop before reaching 1 or -1
+ 13
@Not Found
i don't think it is a bug
+ 12
@Vari93
excellent alg
+ 11
@sayan : I didn't get the question initially. Now that I get it, I edited my code.
+ 8
@Not Found : Check my code now. It's modified to take any input.
+ 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
+ 4
https://code.sololearn.com/cl8AszaynDuc/?ref=app here it is.
+ 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
+ 4
@Randall Young
well now it isn't šš
+ 4
Here you go
https://code.sololearn.com/cl47721vb8E4/?ref=app
+ 3
both sayan chandra and krishna's code are correct.
+ 3
the mass is with me but not the acceleration :)
https://code.sololearn.com/WJr2VrenqXQC/?ref=app
note: the question says twice odd
+ 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?
+ 2
heres mine
.
https://code.sololearn.com/cnQ9Afxc5rp1/?ref=app
+ 2
it works fine now š
+ 2
@Not found Thank you, I will work on this when I get off work.
+ 2
I did it in C++
https://code.sololearn.com/cz70X4tCRPNE/?ref=app
+ 2
oh, someone did it before me. rip.