Question
Ramanujan numbers. When the English mathematician G. H. Hardy came to visit the Indian mathematician Srinivasa Ramanujan in the hospital one day, Hardy remarked that
- Ramanujan numbers. When the English mathematician G. H. Hardy came to visit the Indian mathematician Srinivasa Ramanujan in the hospital one day, Hardy remarked that the number of his taxi was 1729, a rather dull number. To which Ramanujan replied, No, Hardy! No, Hardy! It is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways.
An integer n is a Ramanujan number if can be expressed as the sum of two positive cubes in two different ways. That is, there are four distinct positive integers a, b, c, and d such that n=a3+b3=c3+d3n=a3+b3=c3+d3. For example 1729=13+123=93+1031729=13+123=93+103.
Write a program
Ramanujan.java
that takes a long integer command-line argument n and printstrue
if it is a Ramanujan number, andfalse
otherwise. To do so, organize your program according to the following public API:public class Ramanujan { // Is n a Ramanujan number? public static boolean isRamanujan(long n) // Takes a long integer command-line arguments n and prints true if // n is a Ramanujan number, and false otherwise. public static void main(String[] args) }
Here are a few sample executions:
~/Desktop/performance> java-introcs Ramanujan 1729 true ~/Desktop/performance> java-introcs Ramanujan 3458 false ~/Desktop/performance> java-introcs Ramanujan 4104 true ~/Desktop/performance> java-introcs Ramanujan 216125 true ~/Desktop/performance> java-introcs Ramanujan 9223278330318728221 true
Your program should take time proportional to n1/3n1/3 in the worst case. It should be fast enough to process any 64-bit long integer in a fraction of a second.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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