Question

create an input file with the numbers shown below and then create an output file with...

create an input file with the numbers shown below and then create an output file with java ?

Polish Peasant Multiplication

About

It takes two numbers, side by side. The left number is multiplied by 2 every time the right number is divided by 2.

50                    30

100                  15

200                  7

400                  3

800                  1

Take out the numbers in the left side that corresponds to an even number to the right side, in this case 30 is the only even number available.

                        

30

100                  15

200                  7

400                  3

800                  1

Add all the left side: This will equal to 1500.

Test the answer by multiplying the first two original numbers

50*30 = 1500

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class PeasantMultipication {

        /**
         * this method will multiply two values 
         * by using peasant multiply method
         * @param n1
         * @param n2
         * @return
         */
        public static int peasantMultiply(int n1,int n2)
        {
                //taken two array list for 
                //right hand side and left hand side
                ArrayList<Integer> leftSide=new ArrayList<Integer>();
                ArrayList<Integer> rightSide = new ArrayList<Integer>();
                
                //dividing the n1 by 2 until it greater than 1
                while(n1>1)
                {
                        //adding the values to leftside of array
                        leftSide.add(n1/2);
                        n1=n1/2;
                }
                /*doubling the number n2 (multiplying by 2 to result)
                 * till the size of leftSide array
                 */
                for(int i=0;i<leftSide.size();i++)
                {
                        rightSide.add(n2*2);
                        n2=n2*2;
                }
                //removing the even no from left side array and on same
                //index from right side array
                ArrayList<Integer> temp = new ArrayList<>();
                for(int i=0;i<leftSide.size();i++)
                {
                        //if divisible by 2 then removing
                        if(leftSide.get(i)%2==0)
                                continue;
                        temp.add(rightSide.get(i));
                }
                //reassigning the value to right side array
                rightSide=temp;
                int mulValue = 0;
                //looping through each variable and adding the values
                for(Integer i : rightSide)
                        mulValue += i;
                //returning the final value of multiplication
                return mulValue;
        }
        //main method for execitung the program
        public static void main(String[] args) {
                
                //multiplying value 1
                System.out.println("146 x 37 : "+peasantMultiply(146, 37)+" , Expected "+(146*37));
                System.out.println("212 x 54 : "+peasantMultiply(212, 54)+" , Expected "+(212*54));

        }

}
Add a comment
Know the answer?
Add Answer to:
create an input file with the numbers shown below and then create an output file with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Draw a Raptor flowchart that inputs 50 integer numbers and accumulates only the input integer num...

    Draw a Raptor flowchart that inputs 50 integer numbers and accumulates only the input integer numbers that are odd and less than 45. Display the final accumulated value before ending the flowchart. Hint: MOD operators both give the remainder when the value on the left of the operator is divided by the operator on the right. Use MOD operator to check whether an integer number is odd or even. number mod 2 returns 1 , then the number is odd....

  • Create a JAVA program that in two numbers from user input, the program then does the...

    Create a JAVA program that in two numbers from user input, the program then does the following arithmetic calculations. The program will run in ascending order if the first number is small and in descending order of the first number is bigger. The program then alternates between addition(ODD) and subtraction(EVEN) depending on the previous number see examples below   [15] Sample run 1: Enter two numbers: -3 2 Output: The following arithmetic calculations were performed : Arithmetic operations = (-3) +...

  • The numbers of hours worked (per week) by 400 statistics students are shown below. Number of...

    The numbers of hours worked (per week) by 400 statistics students are shown below. Number of hours Frequency 0-9 20 10-19 80 20- 29 200 30-39 100 The cumulative percent frequency for 29 hours is 50 75 200 300

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Create a web page like the one shown here. The user can enter a number n,...

    Create a web page like the one shown here. The user can enter a number n, and when the button is clicked, the Harshad numbers between 1 and n are displayed: linking an html file with a javascript file. Count 'em! Find and Count the Niven Numbers Between 1 and: 200 Number of Nivens: 59 Niven Numbers: 1 23456789 10 12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81...

  • Using C++ 1. Create a while loop that counts even numbers from 2 to 10 2....

    Using C++ 1. Create a while loop that counts even numbers from 2 to 10 2. Create a for loop that counts by five (i.e. 0, 5, 10, ...) from 0 to 100 3. Ask for a person's age and give them three tries to give you a correct age (between 0 and 100) 4. Use a for loop to list Celsius and Fahrenheit temperatures. The "C" should be from -20 to 20 and the F should be shown correspondingly...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • Assume that a purely competitive firm has the following schedule of average and marginal costs: Output...

    Assume that a purely competitive firm has the following schedule of average and marginal costs: Output 1 AFC $300 150 100 No от во 60 50 43 38 33 30 AVC $100 75 70 73 80 90 103 119 138 160 ATC $400 225 170 148 140 140 146 156 171 190 MC $100 50 60 80 110 140 180 230 290 360 9 10 e. At a price of $55, the firm would produce units of output. At a...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT