Skill Subsist Impulse Ltd Interview Question
Software DevelopersCountry: India
#include<stdio.h>
double fact(int n)
{
if(n<=1)
return 1;
return n*fact(n-1);
}
int main()
{
int i=0, num =0, n=0,k=0;
printf("enter the no of rows\n");
scanf("%d",&num);
n = num -1;
for(i=0;i<num;i++)
{
for(k=n;k>i;k--)
printf(" ");
for(k=0;k<=i;k++)
{
printf(" %.0f", fact(i)/(fact(i-k)*fact(k)));
}
printf("\n");
}
return 0;
}
You probably just want to replace the factorial function with something like this :
// ZoomBA
FACTORIALS = list(1,1,2,6,24)
def factorial ( n ){
if ( n <= size(FACTORIALS) ) return FACTORIALS[n]
for ( [size(FACTORIALS):n+1] ){
FACTORIALS += FACTORIALS[-1] * $
}
FACTORIALS[n]
}
println( factorial(10) )
- NG October 09, 2016