Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an Erlang function named collatz that takes one argument N . You may assume that N is an integer 1 or larger. The function

Write an Erlang function named collatz that takes one argument N. You may assume that N is an integer 1 or larger. The function should print the Collatz sequence (one number per line) starting with N. For example, collatz( 4 ) should print 4, 2, 1 (on separate lines). collatz( 6 ) should print 6, 3, 10, 5, 16, 8, 4, 2, 1 (on separate lines).

The Collatz sequence is a sequence that is defined like this: Start with any positive integer n. Then, generate a sequence such that the next number in the sequence is equal to 3n + 1 if n is odd, and n / 2 if n is even. The Collatz conjecture says that, no matter what number you start with, the sequence will always reach a value of 1.

Hints: Your base case should be when N is 1 you should stop printing at that point. If N is larger than 1, print N and then call collatz on the appropriate value: 3N + 1 if N is odd, N div 2 if N is even (you should use div to keep N as an integer).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

LO3 Name the seven categories of HR functions.

Answered: 1 week ago