An important part of mathematics is working with functions A
Solution
import java.util.Scanner;
public class MathFunction {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
System.out.println(\"Enter the phase you want to check (1,2,3) : \");
switch(sc.nextInt()){
case 1:
do{
System.out.println(\"Enter the value of \'x\' for f(x) : \");
a = sc.nextInt();
if(a==-1)break;
else if(a < 0){
System.out.println(\"Invalid Input--> Enter +ve : \");
continue;
}
int nextNum = phaseOne(a);
System.out.println(\"Next number from f(x) is : \"+nextNum);
}while(a != -1);
break;
case 2 : System.out.println(\"Enter the value of \'x\' for f(x) : \");
a = sc.nextInt();
phaseTwo(a);
break;
case 3 : phaseThree();
break;
default : System.out.println(\"Invalid Input!!!!!!\");
}
}
private static void phaseThree() {
// how to print the number is not clear....
// from my understanding I am printing the count which can be reduced to 1.
int count = 0;
for(int i = 1; i<=1000000; i++){
if(check(i) == 1){
count++;
}
if(i % 10000 == 0){
System.out.println(\"all \"+count+\" < \"+i+\" works\");
}
}
}
private static int check(int a) {
int b = a;
int nextVal = a;
do{
nextVal = phaseOne(nextVal);
if(nextVal == 1 ||nextVal == b){
break;
}
}while(nextVal!=1);
return nextVal;
}
private static int phaseTwo(int a) {
int b = a;
int nextVal = a;
do{
System.out.print(nextVal);
nextVal = phaseOne(nextVal);
if(nextVal == 1 ||nextVal == b){
break;
}
System.out.print(\" == > \");
}while(nextVal!=1);
System.out.println(\" == > \"+nextVal);
return nextVal;
}
private static int phaseOne(int a) {
if(a%2 == 0){
return (a/2);
}else{
return (3*a+1);
}
}
}

