Go: Collatz steps and peak

A loop that runs until a condition changes can follow a surprising path.

What to build

A program that follows the Collatz rule: if the number is even halve it, otherwise multiply by 3 and add 1, until it reaches 1.

Requirements

  • Read q, then q numbers.
  • For each number n print n: steps S, peak P, where S is how many steps it takes to reach 1 and P is the largest value reached (including n).

Input

Line 1: q. Then q whole numbers.

Output

q lines.

Constraints

1 ≤ q ≤ 20; 1 ≤ n ≤ 10000.

Example

Input:

3
1 6 27

Output:

1: steps 0, peak 1
6: steps 8, peak 16
27: steps 111, peak 9232

Why it works

The loop ends when the value reaches 1. Each round applies the rule once, counts a step, and keeps the largest value seen. int64 makes sure big peaks cannot overflow.

Examples

Example 1 · Input
3
1 6 27
Expected output
1: steps 0, peak 1
6: steps 8, peak 16
27: steps 111, peak 9232
Example 2 · Input
2
7 16
Expected output
7: steps 16, peak 52
16: steps 4, peak 16

Hints

Hint 1

Use a loop that runs while x != 1, and use int64 for x.

Hint 2

Update the maximum after each step with an if.

Solution

Show one correct solution (try it yourself first)
package main

import "fmt"

func main() {
	var q int
	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		var n int64
		fmt.Scan(&n)
		x := n
		peak := n
		steps := 0
		for x != 1 {
			if x%2 == 0 {
				x /= 2
			} else {
				x = 3*x + 1
			}
			steps++
			if x > peak {
				peak = x
			}
		}
		fmt.Printf("%d: steps %d, peak %d\n", n, steps, peak)
	}
}

▶ Open this solution in the compiler

Lesson: Loops with for →