how to solve Fibonacci, prime and composite number and factorial using a single static class in typescirpt.
working link
-------------
https://stackblitz.com/edit/typescript-be1art
these are some frequently asked question such as prime numbers, factorial and Fibonacci series for an interview purpose. I am trying to solve these question using a single static class in typescript.
here the whole code of my static class where I am using a single class to solve these problem's
-------------
https://stackblitz.com/edit/typescript-be1art
these are some frequently asked question such as prime numbers, factorial and Fibonacci series for an interview purpose. I am trying to solve these question using a single static class in typescript.
here the whole code of my static class where I am using a single class to solve these problem's
class MathProblem
{
//Fibonacci series--------------------
static Fibonacci (max)
{
let a:number=0;
let b:number=1;
let f:number=1;
let series=[];
for(let i=0;i<=max;i++)
{
if(i===0)
{
f=0;
}else if(i===1)
{
f=1;
}
else
{
f=a+b;
a=b;
b=f;
}
series.push(f);
}
return series;
}
//factorial-----------------------
static factorial(max)
{
let r=1;
for(var i=1;i<=max;i++)
{
r*=i;
}
return r;
}
//http://www.softschools.com/math/prime_numbers/
static isPrimeNumber(request)
{
for(var i=2;i<request;i++)
{
if(request%i===0)
{
return false;
}
}
return true;
}
}
//getting prime number
//console.log(MathProblem.isPrimeNumber(43));
//getting Fibonacci
//console.log(MathProblem.Fibonacci(10));
//getting isPrimeNumber
//console.log(MathProblem.factorial(10));
Comments
Post a Comment