Question: write a program that inputs two positive integers, m and n, and calculates (m % 1,000,000,000), that is, the remainder of dividing m by

write a program that inputs two positive integers, m and n, and calculates (m

write a program that inputs two positive integers, m and n, and calculates (m" % 1,000,000,000), that is, the remainder of dividing m" by 1,000,000,000. In other words, it prints the last nine digits of m". For example, if the input integers are 2 and 40, it calculates 240 % 1,000,000,000 1,099,511,627,776 % 1,000,000,000 = 511,627,776. Input and output: Your program should input two integers, m and n, between 1 and 1,000,000,000,000,000,000, and print the value of (m" % 1,000,000,000). Screen shot: Command Prompt C:\>java Exercise4 Enter base: 2 Enter exponent: 40 2 40 % 1000000000 = = 511627776 C:\>java Exercise4 Enter base: 2 Enter exponent: 10000000 2^1000000000000000000 % 1000000000 = 787109376 030 000 300 X C:\> Hint: The "brute-force" approach of first calculating m" by an n-iteration loop and then applying the % 1,000,000,000" operation to the result does not work. First, for large values of n, the value of m" may be far too large for the computer memory, even if it is declared as "BigDecimal". Second, an n-iteration loop may take impractically long time.

Step by Step Solution

3.41 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To calculate mn 1000000000 efficiently without running into memory or time issues for the large values of n modular exponentiation can be used Java Pr... View full answer

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 Programming Questions!