Question
PLEASE USE QUEUE DATA STRUCTURE AND IMPLEMENT THE GIVEN ALGORITHM: SAMPLE CODE IS GIVEN BELOW! Srinivasa Ramanujan was an Indian mathematician who became famous for
PLEASE USE QUEUE DATA STRUCTURE AND IMPLEMENT THE GIVEN ALGORITHM:
SAMPLE CODE IS GIVEN BELOW!
Srinivasa Ramanujan was an Indian mathematician who became famous for his intuition for numbers. When the English mathematician G. H. Hardy came to visit him one day, Hardy remarked that the number of his taxi was 1729, a rather dull number. To which Ramanujan replied, No, Hardy! It is a very interesting number. It is the smallest number expressible as the sum of two cubes in two different ways. Since: 1729 = 1^3 + 12^3 = 9^3 + 10^3.
Please finish writing the program Ramanujan.java using a minimum-oriented PriorityQueue to find all such numbers that are less than or equal to N and that each can be expressed as the sum of two cubes in two different ways. In other words, find distinct positive integers i, j, k, and l such that i^3 + j^3 = k^3 + l^3.
Algorithm is working as follows:
1) Initialize a minimum-oriented priority queue with pairs (1,2), (2,3), (3,4), ..., (i, i + 1) such that < cube root of .
2) While the priority queue is nonempty:
remove the pair (i, j) such that i^3 + j^3 is the smallest (this is easily done since we are using a minimum-oriented priority queue)
print the previous pair (k, l) and the current pair (i, j), if ^3 + ^3 = ^3 + ^3 , the previous pair here means the Pair object that has been dequeued out in the last iteration. You can initialize the previous pair to be an arbitrary pair, i.e. Pair(0, 0)
and then if < cube root of , insert (i, j+1) into the priority queue.
SAMPLE CODE:
package hw3;
import java.util.*;
public class Josephus {
public ArrayList
// YOUR CODES
return null; // for compilation. You need to change it.
}
public static void main(String[] args) {
Josephus josephus = new Josephus();
Scanner input = new Scanner(System.in);
System.out.print("Please enter a number for N indicating the total number of people: ");
int N = input.nextInt();
System.out.print("Please enter a number for M indicating the Mth person to be eliminated: ");
int M = input.nextInt();
input.close();
System.out.println("People will be deliminated in the following order: ");
System.out.println(josephus.getJosephus(N, M));
}
}
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