Can someone help me with this python3 problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me with this python3 problem?

If the number n is even divide it by two (n/2), if it is odd multiply it by 3 and add 1 (3n+1). Repeat this process until you get the number 1. Find 10 starting numbers that takes at least 100 iterations to get 1

25th Jan 2021, 10:04 PM
Alumina
6 Answers
+ 2
I got the solution so I will give some hints , Run a for loop with range less than 1000, inside run a (while number>1) loop for each number in that range . now check if the number is divisible by 2 ,if yes divide it like n=n/2 , otherwise n=3n+1. In the while loop maintain a counter variable that will check how many times it ran until number became 1. and you can for each number set the counter to 0 before while loop and then check after while loop if counter is greater than 100 ,if yes append the original number to say a numbers_array till you get 10 numbers with 100 iteration count.
25th Jan 2021, 10:23 PM
Abhay
Abhay - avatar
+ 1
Alumina by performing operations on another variable like num , num=x also you can add a check like this to break the loop after array length is 10. if len(numbers_array)>=10: break elif y >100: numbers_array.append(x)
25th Jan 2021, 10:54 PM
Abhay
Abhay - avatar
27th Jan 2021, 8:12 AM
Pee Frosh
Pee Frosh - avatar
0
Thank you so much i have been trying to solve this for days
25th Jan 2021, 11:07 PM
Alumina
0
No problem :)
25th Jan 2021, 11:08 PM
Abhay
Abhay - avatar
- 1
How can i append the original number now https://code.sololearn.com/c8A11A20a2a1
25th Jan 2021, 10:42 PM
Alumina