how to use BigInt for a modified Fibonacci - java

I am trying to solve this: problem
And by Dynamic programming I am able to solve the problem, here is the code so far:
import java.util.Scanner;
public class Dy
{
static long[] storage = new long[20];
public static void main(String[] args)
{
Scanner sx = new Scanner(System.in);
storage[0] = sx.nextInt();
storage[1] = sx.nextInt();
int x = sx.nextInt();
System.out.println(beast(x-1));
}
public static long beast(int n)
{
if(n==0)
return storage[n];
if(n==1)
return storage[n];
if(storage[n]!=0)
return storage[n];
storage[n] = (beast(n-1)*beast(n-1)) + beast(n-2);
return storage[n];
}
}
But the real trouble here is , if n is say around 20, even long data type won't hold the value, so the choice is obviously BigInteger , but as a learner, I was wondering how to use BigInteger with Arrays in Java? Any help will be much appreciated, thanks!

You could use a Map<Integer, BigInteger> and something like,
static Map<Integer, BigInteger> storage = new HashMap<>();
public static void main(String[] args) {
Scanner sx = new Scanner(System.in);
// Add 0 and 1.
storage.put(0, BigInteger.valueOf(sx.nextInt()));
storage.put(1, BigInteger.valueOf(sx.nextInt()));
int x = sx.nextInt();
System.out.println(beast(x - 1));
}
public static BigInteger beast(int n) {
if (!storage.containsKey(n)) {
BigInteger t = beast(n - 1);
storage.put(n, t.multiply(t).add(beast(n - 2)));
}
return storage.get(n);
}

Related

Why do it succeed to deal with the memory size of long data type like this?

I am new to dealing with very large integers in java, in regards to mathematical questions.
This is my answer to a solution of cutting papers into 1*1 squares.
public static void main(String[] args) {
long result = solve(841251657, 841251657);
System.out.println(result);
}
static long solve(int n, int m) {
long r = n*m - 1;
return r;
}
The output is 1810315984, which is far from the expected output of 707704350405245648.
However, both ways below:
Either replacing the mathematical calculation for long with BigInteger,
static long solve(int n, int m) {
BigInteger r = BigInteger.valueOf(n).multiply(BigInteger.valueOf(m));
return r.longValue() - 1;
}
Or inserting the input manually(not sure if it is the actual reason),
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long m = in.nextLong();
long n = in.nextLong();
long cuts = m*n-1;
System.out.println(cuts);
}
can both output the expected answer.
It would be really nice if I could know the reasons. Thank you so much.
The value of n * m is getting overflowed from int limit and therefore you can cast one of n or m to long in order to turn the result of the multiplication to long.
public class Main {
public static void main(String[] args) {
long result = solve(841251657, 841251657);
System.out.println(result);
}
static long solve(int n, int m) {
long r = (long)n * m - 1;
return r;
}
}
Output:
707704350405245648
It's important for you to know that when the value of an int overflows, it starts again from its minimum limit e.g.
public class Main {
public static void main(String[] args) {
int x = Integer.MIN_VALUE;
System.out.println(Integer.MIN_VALUE);
System.out.println(x + 1);
System.out.println(x + 2);
}
}
Output:
-2147483648
-2147483647
-2147483646

How do i incorporate ADT into this program

I have a task to write a program that takes in a profit score and multiplies it by 2, takes hard work score and multiplies it by 5, adds the two together, divides by 7 and then multiplies by 5000 to give a "bonus".
This would be very simple but the specification states i must use at least 9 methods not including main, use functions and getter/setter methods. The rest i can do but i don't know how to use ADT in a program like this.
This is what i've got so far...it does what its supposed to but its missing ADT.
import java.util.Scanner;
class bonus {
public static void main(String[] args) {
int i1 = inputProfit();
int w1 = inputWork();
int p1 = performanceScore(i1, w1);
int f1 = finalbonus(p1);
output(f1);
System.exit(0);
}//end main method
public static int inputProfit() {
Scanner scanner = new Scanner(System.in);
System.out.println("Profit score?");
int profitScore = scanner.nextInt();
return profitScore;
}
public static int inputWork() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("Hard work score?");
int workScore = scanner1.nextInt();
return workScore;
}
public static int profit(int profitScore) {
profitScore = profitScore*2;
return profitScore;
}
public static int work(int workScore) {
workScore = workScore*5;
return workScore;
}
public static int performanceScore(int profitScore, int workScore) {
int p = profit(profitScore);
int w = work(workScore);
int performance = ((p + w)/7);
return performance;
}
public static int finalbonus(int performance) {
int payOut = performance * 5000 ;
return payOut;
}
public static void output(int payOut) {
System.out.println("Your bonus is " + payOut + " pounds");
}
}//end class bonus

Array Program for Java

package myProjects;
import java.util.*;
public class Acm3 {
static int numberOfFriends;
static int rowOfChocolate;
static int columnOfChocolate;
static String division;
static Scanner myScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("How many friends you want to distribute?");
numberOfFriends = myScanner.nextInt();
System.out.println("How do you want to distribute?");
division = myScanner.next();
String[] myArray = division.split("\\s");
int[] myArray1 = new int[numberOfFriends];
int i;
for (i = 0; i <= numberOfFriends; i++); {
myArray1[i] = Integer.parseInt(myArray[i]);
System.out.print(myArray1[i]);
}
}
}
I am trying to transfer String array to Int Array.I have a string array and want to convert each elements into int and finally into array. Anyone who can help?
Thanks in advance
int[] intValues = Arrays.asList(myArray)
.stream()
.mapToInt(Integer::parseInt)
.toArray();
System.out.println(Arrays.toString(intValues));

Return value of a function

I am trying to write code.It takes 3 input value 10 4 3.As you can see kdx value is 0.when it call function hello, value of qdx is changing to 11, But it should be 0.And return value is also 11.Do you have any idea why it is happening?
import java.util.Scanner;
import java.io.FileNotFoundException;
public class test1 {
private static final int FORWARD = 1;
private static final int BACKWARDS = -1;
private static Scanner scn = new Scanner(System.in);
public static void main(String args[]) throws FileNotFoundException {
int N = scn.nextInt();
int k = scn.nextInt();
int m = scn.nextInt();
while (N != 0) {
boolean[] offQueue = new boolean[N];
int offCount = 0;
int kdx = 0;
int mdx = N - 1;
kdx = hello(k, offQueue, kdx, FORWARD);
System.out.println(kdx);
}
}
private static int hello(int q, boolean[] offQueue, int qdx, int direction) {
return qdx;//Problem is here
}
}
I cannot reproduce your problem. Are you sure that you are using your last version when you run it? You can as well try to debug your application with Eclipse.

Working with really big numbers

please have a look at the following code
import java.util.ArrayList;
import java.util.List;
public class Big
{
static int primeNumber = 2;
public static void main(String[]args)
{
int numberDevident = 147;
int left=0;
int result=0;
List numbers = new ArrayList();
while(true)
{
result = numberDevident/primeNumber;
left = numberDevident%primeNumber;
if(left!=0)
{
primeNumber++;
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber);
}
}
}
}
This code find the prime factors of a given number (Variable "numberDevident" in the code"). But, there is a case, that is, the given number is 600851475143
It is no way matching to int, long or double. How can I solve this using this much of a big number? Please help
Here I am doing the same with BigInteger
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class Problem3
{
static BigInteger primeNumber = new BigInteger("2");
static BigInteger zero = new BigInteger("0");
static BigInteger add = new BigInteger("1");
public static void main(String[]args)
{
BigInteger numberDevident = new BigInteger("147");
BigInteger left= new BigInteger("0");;
BigInteger result=new BigInteger("0");;
List numbers = new ArrayList();
while(true)
{
result = numberDevident.divide(primeNumber);
left = numberDevident.remainder(primeNumber);
if(left!=zero)
{
primeNumber.add(add);
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber.toString());
}
}
}
}
still no good, it is not displaying anything. Please help.
You might want to take a look at BigInteger.

Categories