Results 1 to 4 of 4
  1. #1
    ohad is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default hi, i have a problem

    I wanted to write a code for catalan numbers, like that: get n: 10
    1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786
    but from some reason when I put the number 10 the last number "58786" become "-62"
    can u tell me how to fix this ?
    thx

    Java Code:
    package first;
    import java.util.*;
    public class fhghjjk {
    	static Scanner sc = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		long x, i,catalan;
    		
    		System.out.println("put in a number:");
    		x = sc.nextInt();
    		while(x < 1){
    			System.out.println("wrong number !, try again:");
    			x = sc.nextLong();
    		}
    			
    			System.out.println("1");
    		for(i=1;i<x+2;i++){
    			
    			cat(i);
    			
    		}
    		}
    	
    public static long f(long fa){
    		long i,sum = fa;
    		for(i = fa-1;i >0;i--){
    			sum = sum * i;
    		}
    		return sum;
    	}
     public static void cat (long num){
    	 long n = num,cat;
    		cat = f(2*n);
    		cat = cat / (f(n+1)*f(n));
    		System.out.println(cat);
     }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: hi, i have a problem

    Print out values of f(n) for different values of n and see for yourself ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    ohad is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default Re: hi, i have a problem

    when its smaller than 10 its olright. but when its larger than 10 its start makeing problems..

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: hi, i have a problem

    Quote Originally Posted by ohad View Post
    when its smaller than 10 its olright. but when its larger than 10 its start makeing problems..
    You're probably trying to calculate 20! (which is 2432902008176640000, which is larger than the maximum long value). You have to switch to the BigInteger type for those calculations or try a more clever approach.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 5
    Last Post: 11-11-2015, 12:16 PM
  2. Replies: 0
    Last Post: 11-07-2012, 12:44 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •