0 Members and 1 Guest are viewing this topic.
#include <stdio.h>main() { // This program was coded by David // This program calculates Catalan Numbers and the opposite too printf("Choose:\n"); printf("1: Calculate Catalan Number of given number\n"); printf("2: Calculate original number of Catalan Number\n"); int answer; scanf("%d",&answer); if (answer == 1) { // Returns the catalan number printf("Enter original number: "); int input; scanf("%d",&input); int catalanNumber = factorial(2*input)/(factorial(input+1)*factorial(input)); printf("The catalan number is: %d",catalanNumber); } if (answer == 2) { // Returns the original number printf("Not finished for now."); //printf("Enter catalan number: "); //int catalan; //scanf("%d,&cataln); } getch(); return 0;}int factorial( int n ){ if ( n <= 1 ) return 1; else return n * factorial( n-1 );}
The Catalan number of 5 is 42
For doing the opposite you can go the naive or brute force way: calculate catalan numbers until it match (do not forget to handle bad inputs).I am thinking in a better approach but the factorial is dragging me off.Offtopic: cool so much recent interest on mathematics here.
Quote from: Galandros on January 30, 2011, 06:23:28 pmFor doing the opposite you can go the naive or brute force way: calculate catalan numbers until it match (do not forget to handle bad inputs).I am thinking in a better approach but the factorial is dragging me off.Offtopic: cool so much recent interest on mathematics here. A loop that goes through all of them would be possible, but quite slow :S
#include <stdio.h>main() { // This program was coded by David // This program calculates Catalan Numbers and the opposite too printf("Choose:\n"); printf("1: Calculate Catalan Number of given number\n"); printf("2: Calculate original number of Catalan Number\n"); int answer; scanf("%d",&answer); if (answer == 1) { // Returns the catalan number printf("Enter original number: "); int input; scanf("%d",&input); int catalanNumber = factorial(2*input)/(factorial(input+1)*factorial(input)); printf("The catalan number is: %d",catalanNumber); } if (answer == 2) { // Returns the original number printf("Enter catalan number: "); int catalan; scanf("%d",&catalan); int i; for(i = 0; i <= catalan; i++) { if ((factorial(2*i)/(factorial(i+1)*factorial(i))) == catalan) { printf("The original number is: %d",i); break; } } } getch(); return 0;}int factorial( int n ){ if ( n <= 1 ) return 1; else return n * factorial( n-1 );}
#include <iostream>#include <stdio.h>using namespace std;int factorial(int n);int main() { // Coded by David printf("Choose:\n"); printf("1: Calculate Catalan Number of given number\n"); printf("2: Calculate original number of Catalan Number\n"); int answer; scanf("%d",&answer); if (answer==1) { printf("Enter input number: "); int input; scanf("%d",&input); int catalanNumber = factorial(2*input)/(factorial(input+1)*factorial(input)); printf("The catalan number is: %d",catalanNumber); getchar(); } if (answer==2) { printf("Enter output number: "); int input; scanf("%d",&input); int i; for (i=0;i<=input;i++) { if ((factorial(2*i)/(factorial(i+1)*factorial(i))) == input) { printf("The original number is: %d",i); getchar(); break; } } } getchar(); return 0;}int factorial(int n) { if ( n <= 1 ) return 1; else return n * factorial( n-1 );}
#include <iostream>using namespace std;int factorial(int n);int main() { // Coded by David cout << "Choose:\n"; cout << "1: Calculate Catalan Number of given number\n"; cout << "2: Calculate original number of Catalan Number\n"; int answer; cin >> answer; if (answer==1) { cout << "Enter input number: "; int input; cin >> input; int catalanNumber = factorial(2*input)/(factorial(input+1)*factorial(input)); cout << "The catalan number is: " << catalanNumber; getchar(); } if (answer==2) { cout << "Enter output number: "; int input; cin >> input; int i; for (i=0;i<=input;i++) { if ((factorial(2*i)/(factorial(i+1)*factorial(i))) == input) { cout << "The original number is: "<< i; getchar(); break; } } } getchar(); return 0;}int factorial(int n) { if ( n <= 1 ) return 1; else return n * factorial( n-1 );}
ooh, recursive functions are always fun. I always like adding a answer == 42 type thing. ;-) Anyway, does this handle negatives correctly?
def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n-1)+fib(n-2)
Quote from: graphmastur on February 20, 2011, 06:04:15 pmooh, recursive functions are always fun. I always like adding a answer == 42 type thing. ;-) Anyway, does this handle negatives correctly?Recursive functions are sweet!Code: [Select]def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n-1)+fib(n-2)The fibonacci function was the first one I learnt Not sure if it handles negatives right, though, I have to test it yet.
No graphmastur, the negative numbers is about the catalan program, not the fibonacci.
Quote from: Scout on February 21, 2011, 07:44:24 amNo graphmastur, the negative numbers is about the catalan program, not the fibonacci.Ah, okay, my bad. I just figured they might try and pass a negative number and I would hate for the routine to be unable to handle it.
Quote from: graphmastur on February 21, 2011, 07:47:39 amQuote from: Scout on February 21, 2011, 07:44:24 amNo graphmastur, the negative numbers is about the catalan program, not the fibonacci.Ah, okay, my bad. I just figured they might try and pass a negative number and I would hate for the routine to be unable to handle it.Nobody who knows what the fibonacci sequence is would input negative numbers. In fact, any sequence, a sequence can't have it's -1th member xD