Given a positive integer n, the following rules will always create a sequence that ends with 1, called the hailstone sequence: If n is even, divide it by 2 If n is odd, multiply it by 3 and add 1 (i.e. 3n +1) Continue until n is 1 Write a program that reads an integer as input and prints the hailstone sequence starting with the integer entered. Format the output so that ten integers, each separated by a tab character (\t), are printed per line. The output format can be achieved as follows: print(n, end='\t') Ex: If the input is: 25 the output is: 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1   My Code:  n = int(input()) print(n, end='\t') i = 1 while n > 1:     if not i % 10:         print(n, end='\n')     if n % 2 == 0:         n = n//2         print(n, end='\t')     else:         n = 3*n+1         print(n, end='\t')     i += 1 print()   Output: Screenshot attached!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

4.15 LAB: Hailstone sequence

 

Given a positive integer n, the following rules will always create a sequence that ends with 1, called the hailstone sequence:

  • If n is even, divide it by 2
  • If n is odd, multiply it by 3 and add 1 (i.e. 3n +1)
  • Continue until n is 1

Write a program that reads an integer as input and prints the hailstone sequence starting with the integer entered. Format the output so that ten integers, each separated by a tab character (\t), are printed per line.

The output format can be achieved as follows:
print(n, end='\t')

Ex: If the input is:

25

the output is:

25 76 38 19 58 29 88 44 22 11

34 17 52 26 13 40 20 10 5 16

8 4 2 1

 

My Code: 

n = int(input())
print(n, end='\t')
i = 1
while n > 1:
    if not i % 10:
        print(n, end='\n')
    if n % 2 == 0:
        n = n//2
        print(n, end='\t')
    else:
        n = 3*n+1
        print(n, end='\t')
    i += 1
print()

 

Output:

Screenshot attached!

=
zyBooks My library > DAT 210: Data Programming Languages home > 4.15: LAB: Hailstone sequence
Latest submission - 3:04 PM CDT on 10/10/22
Only show failing tests
1: Compare output
Output differs. See highlights below.
Input
Your output
Expected output
2: Compare output
25
Your output
25
34
8
Expected output
25
34
8
Input 1
1→
76
17
4
1
76
17
4
Special character legend
38
52
N
38
52
2
19
26
1→
19
26
1
58
13
58
13
Output is nearly correct, but whitespace differs. See highlights below. Special character legend
29
40
29
40
88
20
88
20
44
10
zyBooks catalog
Download this submission
44
10
Total score: 0/10
22
5
22
5
0/2
0/2
? Help/FAQ
Raza Aamir
Transcribed Image Text:= zyBooks My library > DAT 210: Data Programming Languages home > 4.15: LAB: Hailstone sequence Latest submission - 3:04 PM CDT on 10/10/22 Only show failing tests 1: Compare output Output differs. See highlights below. Input Your output Expected output 2: Compare output 25 Your output 25 34 8 Expected output 25 34 8 Input 1 1→ 76 17 4 1 76 17 4 Special character legend 38 52 N 38 52 2 19 26 1→ 19 26 1 58 13 58 13 Output is nearly correct, but whitespace differs. See highlights below. Special character legend 29 40 29 40 88 20 88 20 44 10 zyBooks catalog Download this submission 44 10 Total score: 0/10 22 5 22 5 0/2 0/2 ? Help/FAQ Raza Aamir
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education