1- import [Link].
Scanner;
public class DigitalRootsPrime {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Input: ");
int n = [Link]();
[Link]("Output: ");
boolean firstPrinted = false;
for (int i = 1; i <= n; i++) {
int digitalRoot = findDigitalRoot(i);
if (isPrime(digitalRoot)) {
if (firstPrinted) {
[Link](", ");
}
[Link](i);
firstPrinted = true;
}
}
[Link]();
}
// Calculate digital root of a number
public static int findDigitalRoot(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num;
}
// Check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
if (num <= 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
}
2- import [Link];
public class WordTransformation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Input: ");
String word = [Link]();
// Step 1: Invert ASCII values
String step1 = invertAscii(word);
[Link]("Step 1 (ASCII transformation): \"" + step1 + "\"");
// Step 2: Reverse the string
String step2 = new StringBuilder(step1).reverse().toString();
[Link]("Step 2 (Reversing): \"" + step2 + "\"");
// Step 3: Rotate first character to the last position
String step3 = rotateFirstToLast(step2);
[Link]("Step 3 (Rotating first character to the last position): \""
+ step3 + "\"");
[Link]("Output: \"" + step3 + "\"");
[Link]();
}
// Invert ASCII values
public static String invertAscii(String input) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
char c = [Link](i);
if (c >= 'a' && c <= 'z') {
// Lowercase letters: 'a' -> 'z', 'b' -> 'y', etc.
[Link]((char)('a' + 'z' - c));
} else if (c >= 'A' && c <= 'Z') {
// Uppercase letters: 'A' -> 'Z', 'B' -> 'Y', etc.
[Link]((char)('A' + 'Z' - c));
} else if (c >= '0' && c <= '9') {
// Digits: '1' -> '9', '2' -> '8', etc.
[Link]((char)('0' + '9' - c));
} else {
// Other characters remain unchanged
[Link](c);
}
}
return [Link]();
}
// Rotate first character to the last position
public static String rotateFirstToLast(String input) {
if ([Link]() <= 1) {
return input;
}
return [Link](1) + [Link](0);
}
}
3- import [Link];
public class FibonacciWordPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Input: ");
String word = [Link]();
StringBuilder result = new StringBuilder();
// Generate Fibonacci sequence for the length of the word
int[] fibonacci = generateFibonacci([Link]());
// Apply Fibonacci pattern to the word
for (int i = 0; i < [Link](); i++) {
char c = [Link](i);
int repeats = fibonacci[i];
for (int j = 0; j < repeats; j++) {
[Link](c);
}
// Add a space between characters (except the last one)
if (i < [Link]() - 1) {
[Link](" ");
}
}
[Link]("Output: \"" + [Link]() + "\"");
[Link]();
}
// Generate Fibonacci sequence
public static int[] generateFibonacci(int length) {
int[] fibonacci = new int[length];
// First two Fibonacci numbers are 1
if (length > 0) fibonacci[0] = 1;
if (length > 1) fibonacci[1] = 1;
// Generate rest of the sequence
for (int i = 2; i < length; i++) {
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
return fibonacci;
}
}
4- import [Link];
public class LongestRepeatingSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Input: ");
String input = [Link]();
String result = findLongestRepeatingSubstring(input);
[Link]("Output: \"" + result + "\"");
[Link]();
}
public static String findLongestRepeatingSubstring(String str) {
String longestSubstring = "";
int maxLength = 0;
// Try all possible substrings
for (int i = 0; i < [Link](); i++) {
for (int j = i + 2; j <= [Link](); j++) { // Minimum length of 2
String current = [Link](i, j);
// Check if the substring appears consecutively
String remainingStr = [Link](j);
if ([Link](current)) {
int length = [Link]();
if (length > maxLength) {
maxLength = length;
longestSubstring = current;
}
}
}
}
return longestSubstring;
}
}