-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaths1.java
More file actions
126 lines (112 loc) · 3 KB
/
Copy pathmaths1.java
File metadata and controls
126 lines (112 loc) · 3 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
public class maths1 {
// count digits O(log10n)
public static void count_digits(int x) {
int count = 0;
while (x > 0) {
count++;
x = x / 10;
}
System.out.println(count);
}
// count digits using formula logn+1. O(1)
public static void count_Digits_best(int x) {
int count = (int) Math.log10(x) + 1;
System.out.println(count);
}
// reverse a number O(log10n)
public static void reverse_num(int x) {
int rev = 0;
while (x > 0) {
rev = rev * 10 + x % 10;
x = x / 10;
}
System.out.println(rev);
}
// check number palindrome
public static void check_palindrome(int x) {
int temp = x;
int rev = 0;
while (x > 0) {
rev = rev * 10 + x % 10;
x = x / 10;
}
System.out.println(rev == temp);
}
// find gcd. runs from 1 to min(x,y). O(min(n1,n2))
public static void gcd(int x, int y) {
int gcd = 1;
for (int i = 1; i <= Math.min(x, y); i++) {
if (x % i == 0 && y % i == 0) {
gcd = i;
}
}
System.out.println(gcd);
}
// same TC but executes less times as we start from greatest to smalles.
// O(min(n1,n2)).
public static void gcd_better(int x, int y) {
for (int i = Math.min(x, y); i > 0; i--) {
if (x % i == 0 && y % i == 0) {
System.out.println(i);
break;
}
}
}
// euclidian algo for gcd O(min(n1,n2))
public static int gcd_best(int x, int y) {
while (x > 0 && y > 0) {
if (x > y) {
x = x % y;
} else {
y = y % x;
}
}
if (x == 0) {
return y;
}
return x;
}
// armstrong number O(log10n + 1)
public static void armstrong(int x) {
int sum = 0;
int count = 0;
int temp = x;
int temp1 = x;
while (temp > 0) {
count++;
temp /= 10;
}
while (temp1 > 0) {
int digit = temp1 % 10;
sum += Math.pow(digit, count);
temp1 /= 10;
}
System.out.println(sum == x);
}
// print all divisors O(sqrtN)
public static void print_all_divisors(int x) {
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
System.out.println(i);
if (x / i != i) {
System.out.println(x / i);
}
}
}
}
// check prime O(sqrtN)
public static void is_prime(int x) {
boolean is_prime = true;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
is_prime = false;
break;
}
}
System.out.println(is_prime);
}
public static void main(String[] args) {
System.out.println();
is_prime(7);
}
}