An important part of mathematics is working with functions A

An important part of mathematics is working with functions. A function takes in an input and produces a certain output. One problem that is being worked on is 3x + 1. Consider f(x) = {3x + 1 if x is odd x/2 if x is even Create a function that takes an integer as input and returns an integer as the output of the function base on f(x). This function will only return the next number in the sequence. Keep asking the user until -1 is entered. The number entered will be a positive integer. Create another function that will call (x) until the number you find a cycle or the number returned is 1. That is. 10 = > 5 = > 16 = > 8 = > 4 = > 2 = > 1, stop. A cycle occurs if you get a number back that you have already seen For now, you only need to test for the number that starts the sequence. That is from above, each time you get a number, check that it is not equal to 10. Show the sequence as you find it Keep asking the user until - 1 is entered. The conjecture is if you repeatedly take the output of the function and use it as input, a power of 2 will show up and reduce to the 4, 2, 1 cycle. Write a program that will test all integers from 1 to 1.000.000 to find if they will always reduce to 1. Do not print every number, do incremental messages, i.e. all

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);
       }
      
   }
}

 An important part of mathematics is working with functions. A function takes in an input and produces a certain output. One problem that is being worked on is
 An important part of mathematics is working with functions. A function takes in an input and produces a certain output. One problem that is being worked on is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site