Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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" % 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
Step: 1
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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started