-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2_MyInteger_Factorial.java
More file actions
137 lines (129 loc) · 2.72 KB
/
Q2_MyInteger_Factorial.java
File metadata and controls
137 lines (129 loc) · 2.72 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.math.BigInteger;
public class Q2_MyInteger_Factorial
{
int value;
Q2_MyInteger_Factorial(int a)
{
this.value = a;
}
public int getValue()
{
return this.value;
}
public boolean isEven()
{
return (this.value%2==0);
}
public boolean isOdd()
{
return (this.value%2!=0);
}
public boolean isPrime()
{
int i =2;
if(this.value<2)
{
return false;
}
else{
while(i<value)
{
if(value%i ==0)
{
return false;
}
i++;
}
return true;
}
}
public static boolean isEven(int val)
{
return (val%2==0);
}
public static boolean isOdd(int val)
{
return (val%2!=0);
}
public static boolean isPrime(int val)
{
int i =2;
if(val<2)
{
return false;
}
else{
while(i<val)
{
if(val%i ==0)
{
return false;
}
i++;
}
return true;
}
}
public static boolean isEven(Q2_MyInteger_Factorial m)
{
return (m.value%2==0);
}
public static boolean isOdd(Q2_MyInteger_Factorial m)
{
return (m.value%2!=0);
}
public static boolean isPrime(Q2_MyInteger_Factorial m)
{
int i =2;
if(m.value<2)
{
return false;
}
else{
while(i<m.value)
{
if(m.value%i ==0)
{
return false;
}
i++;
}
return true;
}
}
//parseInt
public static int parseInt(String str)
{
int res = 0;
for(int i =0;i<str.length();i++)
{
int n = str.charAt(i) - '0';
res = (res*10)+n;
}
return res;
}
public static int parseInt(char[] chr)
{
int res = 0;
for(int i =0;i<chr.length;i++)
{
int n = chr[i] - '0';
res = (res*10)+n;
}
return res;
}
public BigInteger factorial(int n) {
// Initialize BigInteger to 1
BigInteger fact = BigInteger.ONE;
// If n is 0 or 1, return 1 (BigInteger representation)
if (n <= 1) {
return fact;
}
// Calculate factorial using BigInteger
while (n > 1) {
fact = fact.multiply(BigInteger.valueOf(n)); // Multiply with BigInteger
n--;
}
return fact; // Return the BigInteger result
}
}