Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Collatz Conjecture Formatting TreasureDragon Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-11-2019, 09:43 PM (This post was last modified: Feb-11-2019, 10:08 PM by TreasureDragon.) Hi everyone, So I figured out how to set up the technical aspect of the Collatz conjecture as well as making it print out the number of steps. However, I am required to print out everystep numerically ordered. So how do I attach a number in front to show which step it was? Here is an example of what we SHOULD have in total: Enter a number : 22 1. 22 is even , divide in half : 11 2. 11 is odd , multiply by 3 and add 1: 34 3. 34 is even , divide in half : 17 4. 17 is odd , multiply by 3 and add 1: 52 5. 52 is even , divide in half : 26 6. 26 is even , divide in half : 13 7. 13 is odd , multiply by 3 and add 1: 40 8. 40 is even , divide in half : 20 9. 20 is even , divide in half : 10 10. 10 is even , divide in half : 5 11. 5 is odd , multiply by 3 and add 1: 16 12. 16 is even , divide in half : 8 13. 8 is even , divide in half : 4 14. 4 is even , divide in half : 2 15. 2 is even , divide in half : 1 Number of steps : 15 Largest number in sequence : 52 This is what I have that only calculates the process and states the number of steps: ```number = int(input("Enter a number: ")) steps = 0 while number > 1: if number % 2 == 0: number = number / 2 steps += 1 elif number % 2 == 1: number = number * 3 + 1 steps += 1 print(f"\nNumber of steps: {steps}") print(f"Largest number in sequence: {largest}") ``` Ok update: I figured out how to get the number to say what it is and the sequence but now I need help with ordering to see what the largest number was... ichabod801 Bunny Rabbit Posts: 2,580 Threads: 59 Joined: Sep 2016 Reputation: 125 Likes received: 831 #2 Feb-11-2019, 10:34 PM Set max_num to number at the start of the loop. Each time through the loop, change max_num to number if number is higher than max_num (note: there's a really simple way to do this with the max function). Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures TreasureDragon Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #3 Feb-11-2019, 10:39 PM (Feb-11-2019, 10:34 PM)ichabod801 Wrote: Set max_num to number at the start of the loop. Each time through the loop, change max_num to number if number is higher than max_num (note: there's a really simple way to do this with the max function). Oh I figured it out! Thank you for your help! :) « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post leading zero number formatting RedSkeleton007 3 87 Jan-27-2019, 04:56 PM Last Post: RedSkeleton007 Formatting cyrillic characters Truman 6 202 Jan-06-2019, 12:48 AM Last Post: Truman formatting question Jonininireland 1 158 Nov-17-2018, 02:53 PM Last Post: Gribouillis LIST FORMATTING Nick1999 2 202 Nov-13-2018, 03:03 AM Last Post: ichabod801 Formatting floats Irhcsa 6 301 Oct-04-2018, 04:23 PM Last Post: volcano63 TypeError: not all arguments converted during string formatting RedSkeleton007 1 1,726 Jul-15-2018, 08:51 PM Last Post: ichabod801 Formatting Code zeaky3000 3 464 May-11-2018, 06:24 AM Last Post: Larz60+ Problems Formatting JSON primowalker 1 351 May-05-2018, 08:28 AM Last Post: Gribouillis Small help with formatting Fumi 1 431 Mar-15-2018, 06:53 PM Last Post: snippsat need bit help in formatting onenessboy 3 494 Mar-15-2018, 06:18 PM Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)