Skip to content

Commit ab5eb79

Browse files
authored
Merge branch 'master' into GoldbachBranch1
2 parents 8f6c975 + 39122a9 commit ab5eb79

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

spotbugs-exclude.xml

-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@
114114
<Match>
115115
<Bug pattern="BL_BURYING_LOGIC" />
116116
</Match>
117-
<Match>
118-
<Bug pattern="PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS" />
119-
</Match>
120117
<Match>
121118
<Bug pattern="UTWR_USE_TRY_WITH_RESOURCES" />
122119
</Match>

src/main/java/com/thealgorithms/maths/Armstrong.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* An Armstrong number is often called a Narcissistic number.
1111
*
1212
* @author satyabarghav
13+
* @modifier rahul katteda - (13/01/2025) - [updated the logic for getting total number of digits]
1314
*/
1415
public class Armstrong {
1516

@@ -20,14 +21,16 @@ public class Armstrong {
2021
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
2122
*/
2223
public boolean isArmstrong(int number) {
24+
if (number < 0) {
25+
return false; // Negative numbers cannot be Armstrong numbers
26+
}
2327
long sum = 0;
24-
String temp = Integer.toString(number); // Convert the given number to a string
25-
int power = temp.length(); // Extract the length of the number (number of digits)
28+
int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits)
2629
long originalNumber = number;
2730

2831
while (originalNumber > 0) {
2932
long digit = originalNumber % 10;
30-
sum += (long) Math.pow(digit, power); // The digit raised to the power of the number of digits and added to the sum.
33+
sum += (long) Math.pow(digit, totalDigits); // The digit raised to the power of total number of digits and added to the sum.
3134
originalNumber /= 10;
3235
}
3336

src/main/java/com/thealgorithms/scheduling/SJFScheduling.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public class SJFScheduling {
1414
protected ArrayList<ProcessDetails> processes;
1515
protected ArrayList<String> schedule;
1616

17+
private static void sortProcessesByArrivalTime(List<ProcessDetails> processes) {
18+
for (int i = 0; i < processes.size(); i++) {
19+
for (int j = i + 1; j < processes.size() - 1; j++) {
20+
if (processes.get(j).getArrivalTime() > processes.get(j + 1).getArrivalTime()) {
21+
final var temp = processes.get(j);
22+
processes.set(j, processes.get(j + 1));
23+
processes.set(j + 1, temp);
24+
}
25+
}
26+
}
27+
}
28+
1729
/**
1830
* a simple constructor
1931
* @param processes a list of processes the user wants to schedule
@@ -22,22 +34,10 @@ public class SJFScheduling {
2234
SJFScheduling(final ArrayList<ProcessDetails> processes) {
2335
this.processes = processes;
2436
schedule = new ArrayList<>();
25-
sortByArrivalTime();
37+
sortProcessesByArrivalTime(this.processes);
2638
}
2739
protected void sortByArrivalTime() {
28-
int size = processes.size();
29-
int i;
30-
int j;
31-
ProcessDetails temp;
32-
for (i = 0; i < size; i++) {
33-
for (j = i + 1; j < size - 1; j++) {
34-
if (processes.get(j).getArrivalTime() > processes.get(j + 1).getArrivalTime()) {
35-
temp = processes.get(j);
36-
processes.set(j, processes.get(j + 1));
37-
processes.set(j + 1, temp);
38-
}
39-
}
40-
}
40+
sortProcessesByArrivalTime(processes);
4141
}
4242

4343
/**

0 commit comments

Comments
 (0)