10 Replies - 523 Views - Last Post: 22 June 2016 - 04:59 AM Rate Topic: -----

#1 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 08:06 AM

0
down vote
favorite
I wanted to create an app to display figurate numbers to some given value (x) according to this equation: (n+d-2)!*((p-2)(n-1)+d))/d!/(n-1)! = x where either d and p are given by the user. At first I did it with "int", but it displayed very litte numbers. Then I changed it to "long", but it still displayed not as much numbers as I wanted. So I decided I should use "BigInteger". Unfortunately, with BigInteger, the app doesn't work at all. It gets stuck, but it works, what I see in task manager (processor is loaded). Here is the code:
import java.math.BigInteger;
import java.util.Scanner;

public class main {
    public static void main (String[] args){
        Scanner in = new Scanner(System.in);
        BigInteger W = BigInteger.valueOf(in.nextLong());
        BigInteger K = BigInteger.valueOf(in.nextLong());
        long x = in.nextLong();     
        BigInteger X = BigInteger.valueOf(x);

        for (int i = 1; i <= x; i++) {
            BigInteger I = BigInteger.valueOf(i);
            BigInteger J = BigInteger.ZERO;

            int poronanie = factorial(J.add(W).subtract(BigInteger.valueOf(2))).multiply((K.subtract(BigInteger.valueOf(2))).multiply(J.subtract(BigInteger.ONE)).add(W)).compareTo(I.multiply(factorial(W)).multiply(factorial(J.subtract(BigInteger.ONE))));

            while (poronanie == -1){
                J = J.add(BigInteger.ONE);
                poronanie = factorial(J.add(W).subtract(BigInteger.valueOf(2))).multiply((K.subtract(BigInteger.valueOf(2))).multiply(J.subtract(BigInteger.ONE)).add(W)).compareTo(I.multiply(factorial(W)).multiply(factorial(J.subtract(BigInteger.ONE))));
            }

            if (poronanie == 0){
                System.out.println(i);
            }
        }
    }

    public static BigInteger factorial(BigInteger n) {
        BigInteger result = BigInteger.ONE;

        while (!n.equals(BigInteger.ZERO)) {
            result = result.multiply(n);
            n = n.subtract(BigInteger.ONE);
        }

        return result;
    }
}

Do You have any idea, why it doesn't work?

Is This A Good Question/Topic? 0
  • +

Replies To: Java app to display figurate numbers doesn't work

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 08:19 AM

Quote

It gets stuck

How are you trying to debug the code to see where it is stuck?
Add some print statements that print out the values of variables as they are changed and used.
Be sure to add some print statements inside all the loops so you can see if what the computer sees when it executes the code.
Was This Post Helpful? 0
  • +
  • -

#3 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 09:30 AM

I set a breakpoint on this line:
result = result.multiply(n);

and it is all fine, but after this line "result" variable is yellow-shaded (I use Eclipse).
When I added this line:
System.out.println(result+" "+n);

after that:
n = n.subtract(BigInteger.ONE);

the output is veeeeery large numbers, both positive and negative and when it comes to "n", it is just getting lower and lower and reaches values under 0, as if the condition of while loop didn't work.
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 09:35 AM

Quote

the condition of while loop didn't work.

Which while loop are you talking about? On line 18 or line 32?

For line 32, what are the values of n as the loop executes? When will the condition ever be true?

This post has been edited by NormR: 21 June 2016 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 10:29 AM

Yes, I mean line 32. n values start from 1 and decrease like 0, -2, -3, -4,... Yes, there is no -1. And the condition is to be true until n reaches 0.
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 10:40 AM

Note on using an equals test in a while loop. Sometimes the code many have a bug that causes the value to skip over the equal value. It is better to use >= or <= to test the loop control variable to keep the loop from going forever.

Quote

n values start from 1
Did you print the value of n to see?
What is the value of n passed to the factorial method?

Quote

decrease like 0, -2, -3, -4,... the condition is to be true until n reaches 0.

Decrementing a value from -2 by one until the value reaches 0 could take a very long time, if ever.

This post has been edited by NormR: 21 June 2016 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 11:07 AM

Quote

Did you print the value of n to see?

Yes.

Quote

What is the value of n passed to the factorial method?

1, when n is initiated from line 16.

Quote

Decrementing a value from -2 by one until the value reaches 0 could take a very long time, if ever.

I think You get me wrong, n should not go less than 0, lines 32-35 should stop looping then.
Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java app to display figurate numbers doesn't work

Posted 21 June 2016 - 11:14 AM

Quote

n should not go less than 0

Try changing the condition in the while loop from n equals 0 to test n >= 0

Quote

n values start from 1 and decrease like 0, -2, -3, -4,...

How can the value of n skip over 0 to get to -2?

What are the input values you are using for testing?

Note: I pre-load the user's input by using the Scanner class's constructor:
 Scanner in = new Scanner(" 1 2 3"); //System.in);  // preload user's input


This post has been edited by NormR: 21 June 2016 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Re: Java app to display figurate numbers doesn't work

Posted 22 June 2016 - 12:13 AM

Quote

Try changing the condition in the while loop from n equals 0 to test n >= 0

n is BigInteger value, so I had to change the code like that:
public static BigInteger factorial(BigInteger n) {
		BigInteger result = BigInteger.ONE;
		
		int porownanie2 = n.compareTo(BigInteger.ZERO);

        while (porownanie2 == 1) {
            result = result.multiply(n);
            n = n.subtract(BigInteger.ONE);
            System.out.print(n+" ");
        }

        return result;
    }

but it still doesn't work. The only change is that n=-1 at one loop.

Quote

Note: I pre-load the user's input by using the Scanner class's constructor

I tried, it doesn't change anything.
Was This Post Helpful? 0
  • +
  • -

#10 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java app to display figurate numbers doesn't work

Posted 22 June 2016 - 03:22 AM

The value of the variable being used to control the loop: porownanie2 must be changed inside of the loop. Otherwise the loop will go forever.

Quote

The only change is that n=-1 at one loop.

If n starts at -1 its value will never be 0. The test made by the while loop should be that n's value is > 0 for the loop to continue so that the loop will not execute if n's value is <= 0.

Quote

I tried, it doesn't change anything.

Strange. I thought that would make the testing easier. Are you still having to enter the 3 values?

What are the input values you are using for testing?

This post has been edited by NormR: 22 June 2016 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 Leftismer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-February 16

Re: Java app to display figurate numbers doesn't work

Posted 22 June 2016 - 04:59 AM

Quote

The value of the variable being used to control the loop: porownanie2 must be changed inside of the loop.

Thanks!
I added this line:
porownanie2 = n.compareTo(BigInteger.ZERO);

after that:
n = n.subtract(BigInteger.ONE);

and now the app works! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1