-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask06.java
166 lines (123 loc) · 3.88 KB
/
Task06.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
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
public class Task06 {
public static void main(String[] args) {
// Problem 1
double a = 2, b = 3, c = 1;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "): hasTwoRoots? " + hasTwoRoots(a, b, c));
a = 0;
b = 2;
c = 1;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "): hasTwoRoots? " + hasTwoRoots(a, b, c));
a = 2;
b = 1;
c = -1;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "): monot? " + monot(a, b, c));
a = 2;
b = 1;
c = 2;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "): monot? " + monot(a, b, c));
a = 2;
b = 1;
c = 2;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "):maxEl = " + maxEl(a, b, c));
a = 2;
b = 2;
c = 4;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "):maxEl = " + maxEl(a, b, c));
a = -2;
b = 1;
c = -3;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "):numPos = " + numPos(a, b, c));
a = -2;
b = 1;
c = 3;
System.out.println("(a, b, c,)=(" + a + ", " + b + ", " + c + "):numPos = " + numPos(a, b, c));
System.out.println("\n");
// Problem 2
int x = 2, y = 3, z = 1;
System.out.println("Max of " + x + ", " + y + ", " + z + " is " + maxOfThree(x, y, z));
for (int i = 12; i < 16; ++i) { // We defined variable 'a' before that is why I define new variable 'i'
System.out.println("Greatest divisor of " + i + " is " + greatestDivisor(i));
}
for (int m = 11, n = 5; m >= 3; m -= 2, n += 2) {
if (areRelativelyPrime(m, n)) {
System.out.println(m + " and " + n + " are relatively prime");
}
}
for (int m = 2; m <= 100; ++m) {
if (isPerfect(m)) {
System.out.println(m + " is perfect");
}
}
}
// Problem 1 Classes
public static boolean hasTwoRoots(double a, double b, double c) {
double discriminant = ((b * b) - (4 * a * c));
if (discriminant <= 0)
return false;
else
return true;
}
public static boolean monot(double a, double b, double c) {
if ((a > b && b > c) || (a < b && b < c))
return true;
else
return false;
}
public static double maxEl(double a, double b, double c) {
if ((a > b) && (a > c))
return a;
else if ((b > a) && (b > c))
return b;
else
return c;
}
public static int numPos(double a, double b, double c) {
int counter = 0;
if (a > 0)
counter++;
if (b > 0)
counter++;
if (c > 0)
counter++;
return counter;
}
// Problem 2 Classes
static int maxOfThree(int a, int b, int c) {
if ((a > b) && (a > c))
return a;
else if ((b > a) && (b > c))
return b;
else
return c;
}
static int greatestDivisor(int n) {
int gcd = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0)
gcd = i;
}
return gcd;
}
static boolean areRelativelyPrime(int a, int b) {
int gcd = 0;
for (int i = 1; i < (a > b ? a : b); i++) {
if ((a % i == 0) && (b % i == 0))
gcd++;
}
if (gcd == 1)
return true;
else
return false;
}
static boolean isPerfect(int n) {
int perfect = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0)
perfect += i;
}
if (perfect == n)
return true;
else
return false;
}
}