Please write in text and not on a sheet of paper thanks. Write a Java program th
ID: 3770415 • Letter: P
Question
Please write in text and not on a sheet of paper thanks. Write a Java program that implements the following problem. Some numbers are cyclic, that is, they repeat some pattern of themselves when multiplied by certain other numbers
.For example, consider 142,857:
142857 × 1 = 142857
142857 × 2 = 285714
142857 × 3 = 428571
142857 × 4 = 571428
142857 × 5 = 714285
142857 × 6 = 857142
However, the pattern breaks with 7, as 142857 × 7 is 999999. True cyclic numbers are rare; after 142857 the next cyclic number is 0588235294117647 which has 16 digits (notethe leading zero; such leading zeros can be important in cyclic numbers).
Rather than play with multi-digit numbers, here you are presented with an initial number, no greater than 10 digits,followed by five additional numbers. The task is to determine whether or not the product of the original numbertimes each of the other five is a cyclic pattern of the original.
Sample execution:
The first input value is the number that you will perform calculations on. Your program will read thisnumber in as well as the five multiples before the computations are performed. You must store the fivemultiples into an array called ‘multiples’ and then use the array elements to perform the calculationsdescribed above.
Your program will then return the resulting TRUE and FALSE statements as results of the multiplecomputations performed on the first value. If the result is cyclical, the answer will be TRUE.
Sample input:
142857
6
2
7
5
1
2
Sample output
TRUE
TRUE
FALSE
TRUE
FALSE
Please write in text and not on a sheet of paper, and make sure you program works...thanks.
Explanation / Answer
Complete Program:
// File: Cyclic.java
import java.util.*;
public class Cyclic
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] multiples = new int[5];
System.out.print("Enter a number: ");
String str = input.next();
while(str.length() > 10)
{
System.out.print("Enter a number (maximum of 10 digits): ");
str = input.next();
}
long number = Long.parseLong(str);
System.out.print("Enter 5 multiples: ");
for(int i = 0; i < 5; i++)
multiples[i] = input.nextInt();
System.out.println();
for(int i = 0; i < 5; i++)
{
System.out.println("For multiple " + multiples[i] + ": " + isCyclic(number, multiples[i]));
}
}
public static boolean isCyclic(long number, int mul)
{
for(int i = 1; i <= mul; i++)
{
long result = number * i;
if(!isSamePattern(number, result))
return false;
}
return true;
}
public static boolean isSamePattern(long number, long result)
{
String str1 = "" + number;
String str2 = "" + result;
int count1, count2;
for(int i = 0; i < str1.length(); i++)
{
char ch = str1.charAt(i);
count1 = 0;
count2 = 0;
for(int j = 0; j < str1.length(); j++)
{
if(str1.charAt(j) == ch)
count1++;
}
for(int k = 0; k < str2.length(); k++)
{
if(str2.charAt(k) == ch)
count2++;
}
if(count1 != count2)
return false;
}
return true;
}
}
Sample Output: