Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA problem Task : Loops public static int gcd(int a, int b) Given two positive integers a and b, find and return the greatest common

JAVA problem

Task : Loops

public static int gcd(int a, int b)

Given two positive integers a and b, find and return the greatest common divisor. This is the largest number that evenly divides both inputswith no remainder. (It cannot be larger than the smaller of the two numbers).

public static int lcm(int a, int b)

Given two positive integers a and b, find and return the least common multiple. This is the smallest number that can be divided by the two inputs evenly with no remainder. (It cannot be smaller than either of the two numbers).Hint: the least common multiple can be calculated quickly as:

lcm(a,b) = |a*b| / gcd(a,b) 

public static int collatzLength(int n)

Collatz Sequences are an interesting sequences of numbers. Given a starting positive integer n, the sequence is generated by the following rules:

when a number in the sequence is even, the next number is exactly half that value.

when a number in the sequence is odd, the next number is exactly three times that value, plus one.

the number one is always the last value in the sequence (stop generating values when you get to one).

Since 1937, humanity has wondered if all sequences terminate (reach one). We think they do, but it hasn't been proven, and we don't even think our current Mathematics are sufficient to prove the conjecture.

All you've got to do is report back the length of a sequence that begins with the given parameter n. You are not creating the sequence of values, you just need to trace through and find how many values are in it before it reaches one. I promise to only test with values that do eventually terminate! :)

Examples:

n length sequence
1 1 1
4 3 4,2,1
5 6 5,16,8,4,2,1
7 17 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1

Solutions go here:

public class Task3 { public static int gcd(int a, int b){

throw new RuntimeException("not implemented!"); } public static int lcm(int a, int b){ throw new RuntimeException("not implemented!"); } public static int collatzLength(int n){ throw new RuntimeException("not implemented!"); } }

Tester:

import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class Task3Tests { public static void main(String args[]){ org.junit.runner.JUnitCore.main("Task3Tests"); } @Test public void task3_lcm_1() { assertEquals( 60, Task3.lcm( 20, 30)); } @Test public void task3_lcm_2() { assertEquals( 30, Task3.lcm( 10, 30)); } @Test public void task3_lcm_3() { assertEquals( 4290, Task3.lcm( 65, 330)); } @Test public void task3_lcm_4() { assertEquals( 4290, Task3.lcm( 330, 65)); } @Test public void task3_lcm_5() { assertEquals( 323, Task3.lcm( 17, 19)); } @Test public void task3_lcm_6() { assertEquals( 210, Task3.lcm( 10, 21)); } @Test public void task3_lcm_7() { assertEquals( 75, Task3.lcm( 25, 15)); } @Test public void task3_lcm_8() { assertEquals( 4, Task3.lcm( 1, 4)); } @Test public void task3_lcm_9() { assertEquals( 342, Task3.lcm( 18, 19)); } @Test public void task3_lcm_10(){ assertEquals( 1332, Task3.lcm( 333, 444)); } @Test public void task3_gcd_01(){ assertEquals( 30, Task3.gcd( 150, 60)); } @Test public void task3_gcd_02(){ assertEquals( 30, Task3.gcd( 60, 150)); } @Test public void task3_gcd_03(){ assertEquals( 6, Task3.gcd( 78, 18)); } @Test public void task3_gcd_04(){ assertEquals( 78, Task3.gcd( 2730, 858)); } @Test public void task3_gcd_05(){ assertEquals( 10, Task3.gcd( 20, 30)); } @Test public void task3_gcd_06(){ assertEquals( 40, Task3.gcd( 120, 320)); } @Test public void task3_gcd_07(){ assertEquals( 1, Task3.gcd( 367, 463)); } @Test public void task3_gcd_08(){ assertEquals( 30, Task3.gcd( 150, 60)); } @Test public void task3_gcd_09(){ assertEquals( 6, Task3.gcd( 78, 18)); } @Test public void task3_gcd_10(){ assertEquals( 78, Task3.gcd( 858, 2730)); } @Test public void task3_collatzLength_01(){ assertEquals( 1, Task3.collatzLength( 1)); } @Test public void task3_collatzLength_02(){ assertEquals( 2, Task3.collatzLength( 2)); } @Test public void task3_collatzLength_03(){ assertEquals( 8, Task3.collatzLength( 3)); } @Test public void task3_collatzLength_04(){ assertEquals( 6, Task3.collatzLength( 5)); } @Test public void task3_collatzLength_05(){ assertEquals( 9, Task3.collatzLength( 6)); } @Test public void task3_collatzLength_06(){ assertEquals( 17, Task3.collatzLength( 7)); } @Test public void task3_collatzLength_07(){ assertEquals( 7, Task3.collatzLength( 10)); } @Test public void task3_collatzLength_08(){ assertEquals( 13, Task3.collatzLength( 17)); } @Test public void task3_collatzLength_09(){ assertEquals( 21, Task3.collatzLength( 117)); } @Test public void task3_collatzLength_10(){ assertEquals( 12, Task3.collatzLength(2048)); } }

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

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

1. What are the four functions of management?

Answered: 1 week ago

Question

8.7 Evaluate at least five traditional training techniques.

Answered: 1 week ago

Question

8.5 Identify the five-step training process.

Answered: 1 week ago