forked from twowaits/make-pull-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9thOct-3.java
39 lines (29 loc) · 1004 Bytes
/
9thOct-3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
class GcdFact{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
long a=sc.nextLong();
long b=sc.nextLong();
//For a<b the Gcd of fact(a) and fact(b) will always be equal to fact(a)..
if(a<b){
System.out.println("The GCD of the Factorial of "+a+" and "+b+" is :" +factorial(a));
}else{
System.out.println("The GCD of the Factorial of "+a+" and "+b+" is :" +factorial(b));
}
//For Confimation we could check it here.
// System.out.println( "The GCD of the Factorial of "+a+" and "+b+" is :" + gcd( factorial(a) , factorial(b) ));
}
private static long gcd(long a, long b) {
return b==0?a:gcd(b,a%b);
}
private static long factorial(long n) {
long fact=1l;
for(long i=n;i>=1;i--){
fact=fact*i;
if(fact <= 0){
return 0;
}
}
return fact;
}
}