//He went out, bearing his cross, to the place called "The Place of a Skull," which is called in Hebrew, "Golgotha" (John 19:17)
package com.javarush.task.task20.task2025;
/*
ÐлгоÑиÑмÑ-ÑиÑла
*/
import java.util.TreeSet;
public class Solution {
public static long[] getNumbers(long N) {
long[] result = null;
TreeSet numbers = new TreeSet<>();
final int level = 12; // array of levels (ÑÑепенÑ)
long[][] pList = new long[level][level];
int[] lst = new int[20]; // couple of numbers
label:
for (long i = 1; i < N; i++) {
//rejecting the numbers in which the sum of the digits is the same
// receiving the couple of numbers in the form of an array
long x = i;
int lengthCounter = 0;
int current;
int last = Integer.MAX_VALUE;
while (x != 0) {
current = (int) (x % 10);
if ((current != 0 && last != 0) && last < current)
continue label;
last = current;
lst[lengthCounter] = current;
x = x / 10;
lengthCounter++;
}
//counting the degrees, and fill them in the table of degrees
//Find the sum of the digits of the number
long summa1 = 0;
for (int j = 0; j < lengthCounter; j++) {
if (pList[lst[j]][lengthCounter] == 0) {// If the degree array does not already have a value
long a1 = lst[j];
if (a1 != 0 && a1 != 1) { // If the digit is 0 or 1, then there is no point in considering the degree
long a2 = lst[j];
for (int jj = 1; jj < lengthCounter; jj++) //counting degree
a1 *= a2;
}
pList[lst[j]][lengthCounter] = a1;// Add a new value to the array of degrees
}
summa1 += pList[lst[j]][lengthCounter];
}
//receiving the couple of degrees
long xx = summa1;
lengthCounter = 0;
while (xx != 0) {
lst[lengthCounter] = (int) (xx % 10);
lengthCounter++;
xx = xx / 10;
}
//From the received sum we take the sum of degrees, for check on the number of amstrings
long summa2 = 0;
for (int j = 0; j < lengthCounter; j++) {
if (pList[lst[j]][lengthCounter] == 0) { // If the degree array does not already have a value
long a1 = lst[j];
if (a1 != 0 && a1 != 1) {
long a2 = lst[j];
for (int jj = 1; jj < lengthCounter; jj++) //counting the degree
a1 *= a2;
}
pList[lst[j]][lengthCounter] = a1;// Add a new value to the array of degrees
}
summa2 += pList[lst[j]][lengthCounter];
}
//adding the result
if (summa1 == summa2 && summa1 < N)
numbers.add(summa1);
}
result = new long[numbers.size()];
int count = 0;
for (Long l : numbers) {
result[count] = l;
count++;
}
return result;
}
public static void main(String[] args) {
Long t0 = System.currentTimeMillis();
int n = 999999999;
long[] lst = getNumbers(n);
Long t1 = System.currentTimeMillis();
System.out.println("time: " + (t1 - t0) / 1000d + " sec");
System.out.println("memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024) + " mb");
for (int i = 0; i < lst.length; i++) {
System.out.print(lst[i] + ", ");
}
System.out.println();
}
}
/*
ÐлгоÑиÑмÑ-ÑиÑла
ЧиÑло S ÑоÑÑÐ¾Ð¸Ñ Ð¸Ð· M ÑиÑÑ, напÑимеÑ, S=370 и M (колиÑеÑÑво ÑиÑÑ) = 3
РеализоваÑÑ Ð»Ð¾Ð³Ð¸ÐºÑ Ð¼ÐµÑода getNumbers, коÑоÑÑй должен ÑÑеди наÑÑÑалÑнÑÑ
ÑиÑел менÑÑе N (long)
наÑ
одиÑÑ Ð²Ñе ÑиÑла, ÑдовлеÑвоÑÑÑÑие ÑледÑÑÑÐµÐ¼Ñ ÐºÑиÑеÑиÑ:
ÑиÑло S Ñавно ÑÑмме его ÑиÑÑ, возведеннÑÑ
в M ÑÑепенÑ
getNumbers должен возвÑаÑаÑÑ Ð²Ñе Ñакие ÑиÑла в поÑÑдке возÑаÑÑаниÑ.
ÐÑÐ¸Ð¼ÐµÑ Ð¸Ñкомого ÑиÑла:
370 = 3*3*3 + 7*7*7 + 0*0*0
8208 = 8*8*8*8 + 2*2*2*2 + 0*0*0*0 + 8*8*8*8
Ðа вÑполнение даеÑÑÑ 10 ÑекÑнд и 50 ÐРпамÑÑи.
ТÑебованиÑ:
1.âРклаÑÑе Solution должен пÑиÑÑÑÑÑвоваÑÑ Ð¼ÐµÑод getNumbers c одним паÑамеÑÑом Ñипа long.
2.âÐеÑод getNumbers должен бÑÑÑ Ð¿ÑблиÑнÑм.
3.âÐеÑод getNumbers должен бÑÑÑ ÑÑаÑиÑеÑким.
4.âÐеÑод getNumbers должен возвÑаÑаÑÑ Ð¼Ð°ÑÑив ÑиÑел ÑдовлеÑвоÑÑÑÑиÑ
ÑÑÐ»Ð¾Ð²Ð¸Ñ Ð·Ð°Ð´Ð°Ñи.
package com.javarush.task.task20.task2025;
*
ÐлгоÑиÑмÑ-ÑиÑла
*
public class Solution {
public static long[] getNumbers(long N) {
long[] result = null;
return result;
}
public static void main(String[] args) {
}
}
*/