-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberAnalyze.java
More file actions
90 lines (74 loc) · 2.65 KB
/
numberAnalyze.java
File metadata and controls
90 lines (74 loc) · 2.65 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class numberAnalyze {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char choice = 'y'; // Initialize choice to 'y'
System.out.println("This is a program where you can type in an integer n, and after that it will do a bunch of analysis on the integer n and other numbers as well! Just wait and see.");
do {
System.out.print("Enter an integer (n): ");
int n = scanner.nextInt();
scanner.nextLine();
if (n <= 0) {
System.out.println("Invalid input! Please enter a positive integer.");
continue;
}
System.out.println("Prime Factorization Results:");
for (int i = 2; i <= n; i++) {
List<Integer> factors = primeFactors(i);
if (!factors.isEmpty()) {
System.out.println(i + ": " + factors);
}
}
int sum = sumOfFactors(n);
System.out.println("Number Analysis:");
System.out.println("Sum of factors: " + sum);
if (sum < n) {
System.out.println(n + " is a deficient number.");
} else if (sum == n) {
System.out.println(n + " is a perfect number.");
} else {
System.out.println(n + " is an abundant number.");
}
if (n % 2 == 0) {
System.out.println(n + " is an even number.");
} else {
System.out.println(n + " is an odd number.");
}
System.out.print("Do you want to continue (y/n)? ");
choice = scanner.nextLine().charAt(0);
} while (Character.toLowerCase(choice) == 'y');
System.out.println("Program exited.");
}
private static List<Integer> primeFactors(int n) {
List<Integer> factors = new ArrayList<>();
while (n % 2 == 0) {
factors.add(2);
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 2) {
factors.add(n);
}
return factors;
}
private static int sumOfFactors(int n) {
int sum = 1;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i)) {
sum += i;
} else {
sum += (i + n / i);
}
}
}
return sum;
}
}