-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberAnalyze_1.java
More file actions
103 lines (84 loc) · 3 KB
/
numberAnalyze_1.java
File metadata and controls
103 lines (84 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
import java.util.Scanner;
public class numberAnalyze_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean continueOrNot;
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.");
while(true) {
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++) {
int[] factors = primeFactors(i);
if (factors.length > 0) {
System.out.print(i + ": ");
for (int factor : factors) {
System.out.print(factor + " ");
}
System.out.println();
}
}
int sum = sumOfFactors(n);
System.out.println("Number Analysis of "+n+":");
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("Type \"quit\" to quit the program, otherwise the program will go on.");
String quit=scanner.nextLine();
if(quit.equalsIgnoreCase("quit")){
System.out.println("Program exited.");
continueOrNot=false;
System.exit(0);
}
}
}
private static int[] primeFactors(int n) {
int[] factors = new int[n];
int index = 0;
while (n % 2 == 0) {
factors[index++] = 2;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
factors[index++] = i;
n /= i;
}
}
if (n > 2) {
factors[index++] = n;
}
// Trim the array to the actual number of factors
int[] trimmedFactors = new int[index];
System.arraycopy(factors, 0, trimmedFactors, 0, index);
return trimmedFactors;
}
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;
}
}//main