Question: Erlang language - Create a module file named functions2.erl . In this module, write the following functions. Make sure your module name is functions2 ,

Erlang language -

Create a module file named functions2.erl. In this module, write the following functions. Make sure your module name is functions2, and that you export each of these functions.

1. A function named is_multiple that takes two arguments X and Y. The function should return true if X is a multiple of Y, and false if it is not. For example, is_multiple(24, 4) should return true; is_multiple( 35, 6 ) should return false. You may assume that X and Y are integers.

2. A function named power_of_two that takes one argument N. You may assume that N is an integer 0 or greater. The function should return the value of 2N. For example, power_of_two( 0 ) should return 1; power_of_two( 5 ) should return 32.

3. 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.

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).

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!