-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathematicsFoundations.java
More file actions
306 lines (292 loc) · 9.34 KB
/
MathematicsFoundations.java
File metadata and controls
306 lines (292 loc) · 9.34 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.spec.RSAOtherPrimeInfo;
import java.util.ArrayList;
import java.util.Arrays;
public class MatheMatics {
public static BigInteger factorial(BigInteger a){
if(a.equals(BigInteger.ZERO) || a.equals(BigInteger.ONE)){
return BigInteger.ONE;
}
else{
return a.multiply(factorial(a.subtract(BigInteger.ONE)));
}
}
public static String oddEven(int[] a){
String ans = "";
for(int i = 0; i< a.length; i++){
if(a[i] %2 == 0){
ans += "Even";
}
else{
ans += "Odd";
}
}
return ans;
}
public static int PermutationsWithVowelsFixed(String a){
int count = 0;
char[] arr = a.toCharArray();
for(int i = 0 ; i < arr.length; i++){
if(arr[i] == 'a' ||arr[i] == 'e' ||arr[i] == 'i' ||arr[i] == 'o' ||arr[i] == 'u' ){
count++;
}
}
int remaining = arr.length - count;
return smallFactorialHelper(remaining);
}
public static int smallFactorialHelper(int n){
if(n == 1 || n == 0){
return 1;
}
else{
return n * smallFactorialHelper(n - 1);
}
}
public static int additionOfEven(int[] arr){
int ans = arr[0];
for(int i = 1; i< arr.length; i++){
if(i % 2 == 0) {
ans = ans + arr[i];
}
}
return ans;
}
public static int additionOfEvenReversed(int[] arr){
reverseIntArray(arr);
int ans = arr[0];
for(int i = 1; i< arr.length; i++){
if(i % 2 == 0) {
ans = ans + arr[i];
}
}
return ans;
}
public static int[] reverseIntArray(int[] arr){
int i = 0;
int j = arr.length -1;
while (i < j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
return arr;
}
public static void printFloydsAlgo(int n){
/*
n = 6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
*/
System.out.println("Floyds Triangle pattern for "+ n+ " rows :\n"+
"******************");
int startwith = 1;
for(int i = 1; i <= n ; i++){
for(int j = 1; j <= i; j++){
System.out.print(startwith + " ");
startwith++;
}
System.out.println();
}
System.out.println("Floyds Triangle pattern end \n" +
"******************");
}
public static int sumOfN(int n){
return n * ((n + 1)/2);
}
public static void printInReverse(int n){
for(int i = n; i >=1; i--){
System.out.print(i + " ");
}
System.out.println();
}
public static void printInReverseEvenSkipped(int n){
for(int i = n; i >=1; i--){
if(i % 2 == 0){
continue;
}
System.out.print(i + " ");
}
System.out.println();
}
public static int sumOfNInGivenRange(int start, int end){
int ans = 0;
for(int i = start; i <= end ; i++ ){
ans = ans + i;
}
return ans;
}
public static boolean LeapOrNot(int yr){
return (yr%4) == 0 ;
}
public static int AddAllDigits(long n){
int ans = 0;
while(n != 0){
ans += n % 10;
n = n / 10;
}
return ans;
}
public static boolean[] isPalisArms(int n){
boolean pal = false;
boolean arms = false;
int og = n;
int rev = 0;
ArrayList<Integer> lg = new ArrayList<>();
while(n > 0){
int lastdg = n % 10;
lg.add(lastdg);
rev = (rev*10) + lastdg;
n = n /10;
}
if(rev == og)
pal = true;
int sumForArms = 0;
for(int i : lg){
sumForArms = sumForArms + ( i * i * i);
}
if(sumForArms == og)
arms = true;
boolean[] answers = new boolean[]{pal, arms};
return answers;
}
public static int[] reverseNumber(int m){
//1234
int ans = 0;
int count = 0;
while(m > 0){
int lastdigit= m%10; //4
m = m/10;
ans =(ans*10) + lastdigit ; // 0 * 10 + 4 = 4
count++;
}
int[] answers = new int[]{ans, count};
return answers;
}
public static boolean isPrime(int n){
//n=12;
boolean ans = false;
int count = 0;
for(int i = 1; i*i <= n;){ // y, y, y,
if((n % i) == 0){ // y, y, n,
count++; // c1, c3
if((n/i) != i ){ // 12/1 = 12 y, 12/2 = 6 y
count++; // c2, c4
} //
}
i++; // 1..,2..,3..,4
}
if(count == 2) ans = true;
return ans;
}
public static int gcd(int a, int b){
while(a > 0 && b > 0){
if(a > b) a= a % b ;
if( b > a) b = b % a;
}
if(a == 0) return b;
return a;
}
public static int fiboOfN(int n){
if(n == 0 || n == 1) return n;
int a = 0 ;
int b = 1;
int c = 0;
for(int i = 2; i<= n ; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
public static String decToBin(int n){
return Integer.toBinaryString(n);
}
public static String decToBinHardCoded( int n ){
if(n == 0 ) return "0";
if( n == 1) return "1";
String ans = "";
while(n > 0){
if( (n & 1)==1) ans = "1" + ans;
if( (n&1)== 0) ans = "0" + ans;
n = n >> 1;
}
return ans;
}
public static String ZeroAndOnes(String bin) {
//bin 11101101
String ans = "";
char[] arr = bin.toCharArray();
int count = 0;
for (char a : arr) {
if (a == '1') {
count++;
}
if (a == '0') {
ans= ans+ alphabetDig(count);
count = 0;
}
}
if(count > 0) ans = ans+ alphabetDig(count);
return ans;
}
public static char alphabetDig(int a){
return (char) ('A' + a -1);
}
public static boolean isGooglyPrime(int num){
int sum = 0;
while(num > 0){
sum = sum +( num % 10 );
num = num / 10;
}
return isPrime(sum);
}
public static void main(String args[]){
//Mathematcis and cp foundation once in and all
System.out.println("total set bit counts is"+ Integer.bitCount(42));
String a = Integer.toBinaryString(42);
System.out.println("the binar no of " + 42 + " is "+ a );
int number = 10; // Example
BigInteger result = factorial(BigInteger.valueOf(number));
System.out.println("prime factor of " + number+" is "+ result);
while(result.mod(BigInteger.TEN).equals(BigInteger.ZERO)){
result = result.divide(BigInteger.TEN);
}
//here result gets destroyed
System.out.println("the non zero ending of the result is"+result.mod(BigInteger.TEN));
System.out.println("Print if odd or even "+oddEven(new int[]{2,3,4,5,1,5,6,}));
System.out.println("permutation of fixed vowels "+ PermutationsWithVowelsFixed("aezqw"));
System.out.println("addition of even postition elems " + additionOfEven(new int[]{1,2,3,4,5,6})); //9
int[] arr= new int[]{1,2,3,4,5};
reverseIntArray(arr);
System.out.print("reversed array :" );
for (int i = 0; i< arr.length; i++){
System.out.print(arr[i]+ " ");
}
System.out.println();
System.out.println("addititon of reversed even position elems" + additionOfEvenReversed(new int[]{1,2,3,4,5,6}) );
printFloydsAlgo(6);
System.out.println(sumOfN(5));
printInReverse(10);
printInReverseEvenSkipped(12);
System.out.println("sum Of N In Given Range"+sumOfNInGivenRange(5, 5));
System.out.println("is the year leap yr or not "+LeapOrNot(2004));
System.out.println("sum of digits of given number is "+ AddAllDigits(1230456));
System.out.println("reversed number is "+reverseNumber(12345)[0] + " and the count of digits is " +reverseNumber(12345)[1] );
System.out.println("is the number palindrome? : " + isPalisArms(12321)[0] + " and is the number armstrong number? : " + isPalisArms(371)[1] );
System.out.println("is 1313 prime? " +isPrime(1313));
System.out.println("gcd using euclidean's algorithm is " + gcd(12,7));
System.out.println("fibonacci seires of nth numer is : " + fiboOfN(9));
System.out.println("binary form of n is : " + decToBin(54));
System.out.println("binary form of n in hard coded is : " + decToBinHardCoded(54));
System.out.println("digit number of alphabet is : " + alphabetDig(8));
System.out.println("digit number of alphabet is : " + ZeroAndOnes("1101011111111"));
System.out.println("is the sum off digits a prime number" +isGooglyPrime(43));
}
}