import java.io.
File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class numbers {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
File file = new File("numbers.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
nums.add(Integer.parseInt(scanner.nextLine()));
}
scanner.close();
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
// Find the largest sum of 10 consecutive numbers
int largest_sum = 0;
for (int i = 0; i < nums.size() - 10; i++) {
int sum = 0;
for (int j = 0; j < 10; j++) {
sum += nums.get(i + j);
}
if (sum > largest_sum) {
largest_sum = sum;
}
}
System.out.println("Largest sum of 10 consecutive numbers: " +
largest_sum);
// Find the sum of the largest sorted section
int largest_sorted_sum = 0;
for (int i = 0; i < nums.size() - 1; i++) {
// Loop from current i to the end
// Break when the sorted section ends
// Compare the sum to the largest_sorted_sum
int sum = nums.get(i);
boolean ascending = true;
boolean descending = true;
for (int j = i + 1; j < nums.size(); j++) {
if (nums.get(j) > nums.get(j - 1)) {
if (descending) {
break;
}
ascending = true;
sum += nums.get(j);
} else if (nums.get(j) < nums.get(j - 1)) {
if (ascending) {
break;
}
descending = true;
sum += nums.get(j);
} else {
break;
}
}
}
// Sort first
ArrayList<Integer> sorted = selection_sort(nums);
// Print first 10, then ..., then last 10.
System.out.print("Sorted: [");
for (int i = 0; i < 10; i++) {
System.out.print(sorted.get(i) + ", ");
}
System.out.print("..., ");
for (int i = sorted.size() - 10; i < sorted.size(); i++) {
System.out.print(sorted.get(i) + ", ");
}
System.out.println("]");
// Save sorted to numbers2.txt
File file2 = new File("numbers2.txt");
try {
file2.createNewFile();
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
try {
FileWriter writer = new FileWriter(file2);
for (int i = 0; i < sorted.size(); i++) {
writer.write(sorted.get(i) + "\n");
}
writer.close();
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
System.out.println("Wrote to numbers2.txt");
// Then check for the largest prime
int largest_prime = 0;
for (int i = sorted.size() - 1; i >= 0; i--) {
if (is_prime(sorted.get(i))) {
largest_prime = sorted.get(i);
break;
}
}
System.out.println("Largest prime: " + largest_prime);
// Create an array with all unique numbers
// Make another parallel array with the count of each number
ArrayList<Integer> unique = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();
for (int i = 0; i < sorted.size(); i++) {
if (!unique.contains(sorted.get(i))) {
unique.add(sorted.get(i));
counts.add(1);
} else {
int index = unique.indexOf(sorted.get(i));
counts.set(index, counts.get(index) + 1);
}
}
// Find the top 5 most common numbers
int[] top_5 = new int[5];
int[] top_5_counts = new int[5];
for (int i = 0; i < counts.size(); i++) {
int current_count = counts.get(i);
for (int j = 0; j < 5; j++) {
// If the current_count > the jth top_5_count, then insert it
if (current_count > top_5_counts[j]) {
// Shift everything down
for (int k = 4; k > j; k--) {
top_5[k] = top_5[k - 1];
top_5_counts[k] = top_5_counts[k - 1];
}
// Insert
top_5[j] = unique.get(i);
top_5_counts[j] = current_count;
break;
}
}
}
System.out.println("Top 5 common: ");
for (int i = 0; i < 5; i++) {
System.out.println(i + 1 + " -> " + top_5[i] + ": " + top_5_counts[i]);
}
// Find the largest fibonacci number
int largest_fibonacci = 0;
for (int i = sorted.size() - 1; i >= 0; i--) {
if (is_fibonacci(sorted.get(i))) {
largest_fibonacci = sorted.get(i);
break;
}
}
System.out.println("Largest fibonacci: " + largest_fibonacci);
// Find instances of 10 consecutive numbers
// ArrayList<Integer[]> consecutives = new ArrayList<Integer[]>();
// outer: for (int i = 0; i < nums.size() - 10; i++) {
// for (int j = 0; j < 10; j++) {
// // If the next number is not 1 greater than the current number,
then it's not consecutive
// if (nums.get(i + j + 1) != nums.get(i + j) + 1) {
// continue outer;
// }
// }
// // If it reaches here, then it's consecutive
// Integer[] consecutive = new Integer[10];
// for (int j = 0; j < 10; j++) {
// consecutive[j] = nums.get(i + j);
// }
// consecutives.add(consecutive);
// }
// System.out.println("Consecutive numbers: ");
// for (int i = 0; i < consecutives.size(); i++) {
// System.out.print("[");
// for (int j = 0; j < 10; j++) {
// System.out.print(consecutives.get(i)[j] + ", ");
// }
// System.out.println("]");
// }
}
static boolean is_fibonacci(int n) {
int a = 0;
int b = 1;
while (a < n) {
int temp = a;
a = b;
b = temp + b;
}
return a == n;
}
static boolean is_prime(int n) {
if (n < 2) {
return false;
}
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
double sqrt_n = Math.sqrt(n);
for (int i = 3; i <= sqrt_n; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
static ArrayList<Integer> selection_sort(ArrayList<Integer> nums) {
ArrayList<Integer> sorted = new ArrayList<Integer>();
while (nums.size() > 0) {
int min = nums.get(0);
int min_index = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) < min) {
min = nums.get(i);
min_index = i;
}
}
sorted.add(min);
nums.remove(min_index);
}
return sorted;
}
}