mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
commit
72e38936c2
26
misc/Collatz.c
Normal file
26
misc/Collatz.c
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
collatz conjecture: a series for a number n in which if n even then the next number is n/2 ,but if n is odd then the next number is 3n+1.
|
||||
this series continues till it reaches 1*/
|
||||
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int n,curr_no;
|
||||
scanf("%d",&n); //input number
|
||||
curr_no=n; //curr_no stores input number n
|
||||
while(curr_no!=1) //loop till series reaches 1
|
||||
{
|
||||
if(curr_no%2==0) //condition for even number
|
||||
{
|
||||
curr_no=curr_no/2;
|
||||
printf("%d->",curr_no);
|
||||
}
|
||||
else
|
||||
{
|
||||
curr_no=(curr_no*3)+1; //condition for odd number
|
||||
printf("%d->",curr_no);
|
||||
}
|
||||
}
|
||||
printf("1");
|
||||
return 0;
|
||||
}
|
28
misc/catalan.c
Normal file
28
misc/catalan.c
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
code for computing nth catalan number
|
||||
*/
|
||||
#include<stdio.h>
|
||||
long int factorial(int x) //long int for more than 10 factorial
|
||||
{
|
||||
int i;
|
||||
long int fac; //fac stores x factorial
|
||||
fac=x;
|
||||
for(i=1;i<x;i++) //loop to calculate x factorial
|
||||
{
|
||||
fac=fac*(x-i);
|
||||
}
|
||||
return fac; //returning x factorial
|
||||
}
|
||||
int main()
|
||||
{
|
||||
long int f1,f2,f3; //long int for more than 10 factorial
|
||||
int n;
|
||||
float C; //C is catalan number for n;
|
||||
scanf("%d",&n);
|
||||
f1=factorial(2*n);
|
||||
f2=factorial(n+1);
|
||||
f3=factorial(n);
|
||||
C=f1/(f2*f3); //formula for catalan number for n
|
||||
printf("%0.2f",C);
|
||||
return 0;
|
||||
}
|
25
misc/factorial_trailing_zeroes.c
Normal file
25
misc/factorial_trailing_zeroes.c
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
programme for computing number of zeroes at the end of factorial of a given number n
|
||||
*/
|
||||
#include<stdio.h>
|
||||
#include<math.h> //including math.h header file to use pow function
|
||||
int main()
|
||||
{
|
||||
int i,n,test=0,count=0;
|
||||
//taking input number n
|
||||
scanf("%d",&n);
|
||||
|
||||
//looping from 1 till loop break
|
||||
for(i=1;;i++)
|
||||
{
|
||||
test=n/pow(5,i);//division of n by ith power of 5(storing in integer form)
|
||||
if(test!=0) //condition for zeroes at end corresponding individual ith case
|
||||
{
|
||||
count=count+test;
|
||||
}
|
||||
else
|
||||
break; //break the loop for if test=0
|
||||
}
|
||||
printf("%d\n",count);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user