top of page

Program Exercise No. 1: Collatz Sequence

The Collatz sequence of a positive integer n is defined as follows:

a) Start with the integer n (the starting number).

b) If the integer is even, divide it by 2 (integer division) (that is, n←n/2)

c) If the integer is odd, multiply it by 3 and add 1. (that is, n←3n + 1)

d) Repeat the process until n becomes 1.

For example, let n = 5. Then its Collatz sequence is:

5→16→8→4→2→1

Since it takes six iterations, the length of the Collatz sequence for starting number 5 is 6.

As another example, let n = 13. Then its Collatz sequence is:

13→40→20→10→5→16→8→4→2→1

The length of the Collatz sequence for starting number 13 is 10.

Although it is not proven yet, it is thought that all Collatz sequences end in 1.

 

Program Exercise No. 2: Cousin of Collatz Sequence

For this exercise, you will be writing a program in steps very similar to Exercise No. 1, except that you will be testing a sequence called the “Cousin of Collatz”. It is also called the 7x ± 1 problem and is defined as follows:

Start at a positive integer n.

If n is even, divide it by 2 (integer division) (that is n <- n/2)

If n is odd and n % 4 is 1, then multiply it by 7 and add 1 (that is, n <- 7n+1)

If n is odd and n % 4 is 3, then multiply it by 7 and subtract 1 (that is, n <- 7n -1)

Repeat until n becomes 1.

(Note: As you know from your first year, % represents the remainder of an integer division. 15%4 is 3 whereas 17%4 is 1).

For example, if n = 5, then the 7x ± 1 sequence is:

5 ->36->18 ->9->64->32->16 ->8 ->4 ->2 ->1

The sequence can grow pretty large. For example, if n = 235, the sequence is the following (the odd integers are shown in bold):

 

CSCI 2110 Data Structures and Algorithms Fall 2022 Assignment No. 2 Java Solved

$80.00Price
  • CSCI 2110 Data Structures and Algorithms Fall 2022 Assignment No. 2 Java Solution

    Assignment No. 2 Solution.

    Solution File : PDF

    Part 1 and Part 2 complete Java Solution.

bottom of page