In my class I tried to return the sum of: 2^n + 2^(n+1) + 2^(n+2) ... in 2 ways.
Iterative in the first method and recursive in the 2nd one.
This worked as long as numbers weren't too big. Can someone explain to me why those methods return different answers when used with high numbers?
public class Power
{
public static void main(String[] args)
{
System.out.println(iterativ(3));
System.out.println(rekursiv(3));
System.out.println(iterativ(40));
// The recursive one is lower by 10
System.out.println(rekursiv(40));
}
public static int iterativ(int x)
{
int sum = 0;
for (int i = 0; i <= x; i++) {
sum += Math.pow(2, i);
}
return sum;
}
public static int rekursiv(int x)
{
if (x > 0) {
return ((int) Math.pow(2, x) + rekursiv(x - 1));
}
return 1;
}
}
You are using functions that deal with double. You are casting your values to int. Casting values will sooner or later always lead to some inaccurate results, even more so, if you cast from double to int.
The number you were seeing when using 40 as your exponent, was 2147483647, which is in fact Integer.MAX_VALUE, but isn't 2^40. It is rather 2^31-1. The java tutorial has a chapter about the primitive datatypes, which shows you the ranges of each type.
Besides using double you may also want to look at BigDecimal instead.
Related
The purpose of this class is to calculate the nth number of the Lucas Sequence. I am using data type long because the problems wants me to print the 215th number. The result of the 215th number in the Lucas Sequence is: 855741617674166096212819925691459689505708239. The problem I am getting is that at some points, the result is negative. I do not understand why I am getting a negative number when the calculation is always adding positive numbers. I also have two methods, since the question was to create an efficient algorithm. One of the methods uses recursion but the efficiency is O(2^n) and that is of no use to me when trying to get the 215th number. The other method is using a for loop, which the efficiency is significantly better. If someone can please help me find where the error is, I am not sure if it has anything to do with the data type or if it is something else.
Note: When trying to get the 91st number I get a negative number and when trying to get the 215th number I also get a negative number.
import java.util.Scanner;
public class Problem_3
{
static long lucasNum;
static long firstBefore;
static long secondBefore;
static void findLucasNumber(long n)
{
if(n == 0)
{
lucasNum = 2;
}
if(n == 1)
{
lucasNum = 1;
}
if(n > 1)
{
firstBefore = 1;
secondBefore = 2;
for(int i = 1; i < n; i++)
{
lucasNum = firstBefore + secondBefore;
secondBefore = firstBefore;
firstBefore = lucasNum;
}
}
}
static long recursiveLucasNumber(int n)
{
if(n == 0)
{
return 2;
}
if(n == 1)
{
return 1;
}
return recursiveLucasNumber(n - 1) + recursiveLucasNumber(n - 2);
}
public static void main(String[] args)
{
System.out.println("Which number would you like to know from "
+ "the Lucas Sequence?");
Scanner scan = new Scanner(System.in);
long num = scan.nextInt();
findLucasNumber(num);
System.out.println(lucasNum);
//System.out.println(recursiveLucasNumber(num));
}
}
Two observations:
The answer you are expecting (855741617674166096212819925691459689505708239) is way larger than you can represent using a long. So (obviously) if you attempt to calculate it using long arithmetic you are going to get integer overflow ... and a garbage answer.
Note: this observation applies for any algorithm in which you use a Java integer primitive value to represent the Lucas numbers. You would run into the same errors with recursion ... eventually.
Solution: use BigInteger.
You have implemented iterative and pure recursion approaches. There is a third approach: recursion with memoization. If you apply memorization correctly to the recursive solution, you can calculate LN in O(N) arithmetical operations.
Java data type long can contain only 64-bit numbers in range -9223372036854775808 .. 9223372036854775807. Negative numbers arise due to overflow.
Seems you need BigInteger class for arbitrary-precision integer numbers
I wasn't aware of the lucas numbers before this thread, but from wikipedia it looks like they are related to the fibonacci sequence with (n = nth number, F = fibonacci, L = lucas):
Ln = F_(n-1) + F_(n+1)
Thus, if your algorithm is too slow, you could use the closed form fibonacci and than compute the lucas number from it, alternative you could also use the closed form given in the wikipedia article directly (see https://en.wikipedia.org/wiki/Lucas_number).
Example code:
public static void main(String[] args) {
long n = 4;
double fibo = computeFibo(n);
double fiboAfter = computeFibo(n + 1);
double fiboBefore = computeFibo(n - 1);
System.out.println("fibonacci n:" + Math.round(fibo));
System.out.println("fibonacci: n+1:" + Math.round(fiboAfter));
System.out.println("fibonacci: n-1:" + Math.round(fiboBefore));
System.out.println("lucas:" + (Math.round(fiboAfter) + Math.round(fiboBefore)));
}
private static double computeFibo(long n) {
double phi = (1 + Math.sqrt(5)) / 2.0;
double psi = -1.0 / phi;
return (Math.pow(phi, n) - Math.pow(psi, n)) / Math.sqrt(5);
}
To work around the long size limit you could use java BigDecimal (https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html). This is needed earlier in this approach as the powers in the formula will grow very quickly.
I'm trying to find the sum of the Fibonacci sequence in Java, but the run time is taking way too long (or is it suppose to?). This slows down anytime I use an integer past 40.
Note: At 50, a negative value is returned which boggles my mind.
Any advice?
public static void main(String[] args) {
//Find Fibonacci sequence
int sum=getSum(50);
System.out.println("Sum of Fibonacci Numbers is " + sum);
}
static int getSum(int n){
if (n==0) return 0;
if (n==1 || n==2) return 1;
else return getSum(n-1) + getSum(n-2);
}
For n > 2, an invocation of your getSum(n) recursively invokes itself twice. Each of those invocations may recurse further. The total number of method invocations scales as 2^n, and 2^50 is a very large number. This poor scaling reflects the fact that the simple-minded recursive approach ends up needlessly recomputing the same results (e.g. fib(4)) a great many times, and it is why your program slows down so rapidly as you increase n.
The negative return value you get after a certain point arises from exceeding the limits of data type int. You could get a larger limit with a wider data type, presumably long. If that's not enough then you would need to go to something like BigInteger, at a substantial performance penalty.
You need to use long instead of int if you want to calculate the 50th Fibonacci number. The 50th Fibonacci number is 12586269025 and exceeds the maximum value of int (see http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html).
A non-recursive algorithm is likely going to be faster, see http://planet.jboss.org/post/fibonacci_sequence_with_and_without_recursion for the different implementations.
As the others already stated you should use long for the calculated fibonacci value, as the number will get very long very fast.
If your formost priority is performance you could use the following formula:
with
(Idea taken from Linear Algebra lecture, actual formula taken from Wikipedia.)
That way you will get the n-th fibonacci number in constant time (depending on the calculation of the n-th powers in the formula).
The following code calculates the fibonacci sequenc of the first 93 numbers with no waiting time (on my machine):
private static final double SQRT_FIVE = Math.sqrt(5);
private static final double GOLDEN_RATIO = (1 + SQRT_FIVE) / 2;
public static void main(String[] args) {
for(int i = 0; i <= 92; i++) {
System.out.println("fib(" + i + ") = " + calculateFibonacci(i));
}
}
public static long calculateFibonacci(int n) {
double numerator = Math.pow(GOLDEN_RATIO, n) - Math.pow(1-GOLDEN_RATIO, n);
double denominator = SQRT_FIVE;
// This cast should in general work, as the result is always an integer.
// Floating point errors may occur!
return (long)(numerator/denominator);
}
From the 94-th number on the long is no longer sufficent and you need to use BigInteger and fitting math operations, as the double calculations may produce calculation errors with such big numbers.
first, use a long instead of an int, to avoid overflow.
Secondly, use a non-recursive algorithm, as a recursive one exists in exponential time I think. A well designed non-recursive one will solve in linear time (I think).
Example non-recursive
static long getSum(int n){
long[] fibonacci = new long[n];
fibonacci[0] = 1;
fibonacci[1] = 1;
if (n==0) return 0;
if (n==1 || n==2) return 1;
for(int i = 2; i < n;i++){
fibonacci[i] = fibonacci[i-1]+ finonacci[i-2];
}
return fibonacci[n-1];
}
I haven't tested this, but it should work.
If you plan to call this method frequently, it might be prudent to store the array outside of the method, so that it is a simple lookup when doing this. This would provide a constant time solution for numbers that have already been calculated at least once. an example of that is below.
static long[] fibonacci= {1,1};
static long getSum(int n){
if (n==0) return 0;
if (n==1 || n==2) return 1;
int old_length = fibonacci.length;
if(fibonacci.length < (n-1)){
fibonacci = Arrays.copyOf(fibonacci,n);
}else{
return fibonacci[n-1];
}
for(int i = old_length; i < n;i++){
fibonacci[i] = fibonacci[i-1]+ finonacci[i-2];
}
return fibonacci[n-1];
}
Again, the example is untested, so a bit of debugging might be required.
Here is a linear time implementation of the algorithm that uses a constant overhead, instead of linear overhead.
static long getSum(int n){
long currentNum = 0;
long previousNum = 1;
long previousNum2 = 1;
if (n==0) return 0;
if (n==1 || n==2) return 1;
for(int i = 2; i < n;i++){
currentNum = previousNum+ previousNum2;
previousNum2 = previousNum;
previousNum = currentNum;
}
return currentNum;
}
Recursive solutions don't necessarily have to be slow. If you were to use this tail-recursive solution, you'd save up a lot of memory and still achieve great speed (e.g. Fib(10000) runs in 1.1s on my machine).
Here n is the sequence number for which you're calculating Fibonacci number, while f0 and f1 are two accumulators, for previous and current Fibonacci numbers respectively.
public class FibonacciRec {
public static int fib(int n, int f0, int f1) {
if (n == 0) {
return f0;
} else if (n == 1){
return f1;
} else {
return fib(n-1, f1, f0+f1);
}
}
public static void main(String[] args) {
System.out.println(fib(10, 0, 1));
}
}
If you want to keep the recursive approach as is, cache results of calculation in an array or map. When you have calculated one Fibonacci for n, save that result. Then, in your method first see if you have the result and return that if you do. Otherwise, make the recursive call(s). Here's an example: recursion is still used and it is quite fast:
public static Map<Long,Long> cache = null;
public static void main(String[] args) {
cache = new HashMap<Long,Long>();
cache.put(0L,0L);
cache.put(1L,1L);
cache.put(2L,1L);
Long sum=getSum(50L);
System.out.println("Sum of Fibonacci Numbers is " + sum);
}
static Long getSum(Long n){
if (cache.containsKey(n)) { return cache.get(n); }
else {
Long fib = getSum(n-1) + getSum(n-2);
cache.put(n, fib);
return fib;
}
}
I want to find the closest fraction equal to 16/76. Whenever i run this i get 1 no matter what. I am doing this for Java class in school.
public class ClassOne {
public static double limit = 16/76;
public static double difference = 1;
public static double numer = 1;
public static double denom = 1;
public static void main(String[] args)
{
for(int i = 1;i<=100;i++)
{
for(int x = 1;x<=100;x++)
{
double temp = limit-(double)(x/i);
System.out.println((x/i));
if(Math.abs(temp) < difference && x/i != 16/76){difference = temp;numer = x; denom = i;
System.out.println("hi");}
}
}
System.out.println(numer + " " + denom);
}
}
A few problems
Use (double)x/(double)i where needed - probably assign to a temp
difference=temp should be difference=Math.abs(temp) or put the abs on the original temp computation
x/i = 16/76 is subject to floating point errors and so may not be hit when you want. May want to use something like 16*i != 76*x which can be computed in integers. I get 21 100.
You're encountering integer division here:
limit - (double)(x/i)
...and here:
public static double limit = 16/76;
Both x and i are int. The cast here will take effect after the division operation has taken effect.
Change your cast so that it applies immediately to one of the variables instead:
limit - ((double)x)/i
Also, consider wherever else you're doing any quotient that isn't with a floating point number (i.e. has a decimal after it or is explicitly cast to a double) - if you need it to be a floating point number, then use the appropriate cast.
I am trying to get this method I made return the value of x*y as a long. However, it is returning a int. As far as I know specifying in the method header to return a long is what need to be done ?
I am unable to get the required result, what am I missing?
Code
public class Returnpower
{
public long power(int x,int n)
{
int total = x * n;
if(x < 0 && n < 0)
{
System.out.println("X and/or N are not positive");
System.exit(0);
}
return (total);
}
public static void main(String[] args)
{
Returnpower power = new Returnpower();
System.out.println(power.power(99999999,999999999));
}
}
Output
469325057
Thanks
Ben
No, it's returning a long. It's just that you're performing the arithmetic in 32-bit integer arithmetic first. Look at how you're doing the arithmetic:
int total = x * n;
You're not even storing the result as a long, so I don't see how you could expect it to retain a full long value. You need total to be a long - and you've got to make one of the operands a long in order to make the multiplication occur in 64-bit.
To force the multiplication to occur in 64-bit arithmetic, you should cast one of the operands:
long total = x * (long) n;
Alternatively, just get rid of the total variable completely - I would suggest performing argument validation before using the parameters anyway:
public long power(int x, int n)
{
if (x < 0 && n < 0)
{
// Use exceptions to report errors, not System.exit
throw new IllegalArgumentException("x and/or n are negative");
}
return x * (long) n;
}
(Additionally, this clearly isn't performing a power operation in the same way as Math.pow, for example...)
Change int to long
public long power(int x,int n)
{
long xx=x;
long nn=n;
long total = xx * nn;
if(x < 0 && n < 0)
{
System.out.println("X and/or N are not positive");
System.exit(0);
}
return total;
}
Out put
99999998900000001
I am trying to beautify a program by displaying 1.2 if it is 1.2 and 1 if it is 1 problem is I have stored the numbers into the arraylist as doubles. How can I check if a Number is a double or int?
Well, you can use:
if (x == Math.floor(x))
or even:
if (x == (long) x) // Performs truncation in the conversion
If the condition is true, i.e. the body of the if statement executes, then the value is an integer. Otherwise, it's not.
Note that this will view 1.00000000001 as still a double - if these are values which have been computed (and so may just be "very close" to integer values) you may want to add some tolerance. Also note that this will start failing for very large integers, as they can't be exactly represented in double anyway - you may want to consider using BigDecimal instead if you're dealing with a very wide range.
EDIT: There are better ways of approaching this - using DecimalFormat you should be able to get it to only optionally produce the decimal point. For example:
import java.text.*;
public class Test
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("0.###");
double[] values = { 1.0, 3.5, 123.4567, 10.0 };
for (double value : values)
{
System.out.println(df.format(value));
}
}
}
Output:
1
3.5
123.457
10
Another simple & intuitive solution using the modulus operator (%)
if (x % 1 == 0) // true: it's an integer, false: it's not an integer
I am C# programmer so I tested this in .Net. This should work in Java too (other than the lines that use the Console class to display the output.
class Program
{
static void Main(string[] args)
{
double[] values = { 1.0, 3.5, 123.4567, 10.0, 1.0000000003 };
int num = 0;
for (int i = 0; i < values.Length; i++ )
{
num = (int) values[i];
// compare the difference against a very small number to handle
// issues due floating point processor
if (Math.Abs(values[i] - (double) num) < 0.00000000001)
{
Console.WriteLine(num);
}
else // print as double
{
Console.WriteLine(values[i]);
}
}
Console.Read();
}
}
Alternatively one can use this method too, I found it helpful.
double a = 1.99;
System.out.println(Math.floor(a) == Math.ceil(a));
You can use:
double x=4;
//To check if it is an integer.
return (int)x == x;