产生数字的数字到达89

时间:2018-03-07 14:39:13

标签: java algorithm math

好吧,我有以下问题:

通过不断添加数字中的数字的平方来创建数字链,以形成新的数字,直到之前看到它为止。 例如,

25→29→85→89→145→42→20→4→16→37→58→89

32→13→10→1→1

因此,任何到达1或89的链条都会陷入无限循环。最令人惊讶的是,所有数字总是达到1或89。 低于n的起始数将达到89?

EX1。 f(500)= 423时间。

EX2。 f(1000)= 857时间。

EX2。 f(1221)= 1043时间。

我目前的解决方案是:

public static int Solving(int n) {
    int numOf89 = 0;

    int oldN = n;
    int count = 0;
    while (count <= oldN) {
        count++;
        n = is89(n);
        if (n == 89) {

            numOf89++;
        }
    }

    return numOf89;
}



public static int is89(int n) {
    int result = 0;
    int val = 0;
    while (n >= 1) {
        val = n % 10;
        result += val * val;
        n = n / 10;
    }
    return result;
}

如何获得低于n的起始数字到达89?

3 个答案:

答案 0 :(得分:3)

如果您要求查看结果,可以执行以下操作;

首先,您可以使用

打印出您希望在if语句中看到的结果
System.out.println(n);

但是如果你想稍后存储它们,你可以声明一个新的ArrayList并在满足“if”语句时添加数字。

如果这是您要采用的方法,请导入ArrayLists

import java.util.*;

然后在到达方法之前在代码的某些部分创建一个新的ArrayList:

List<> eightyNines = new ArrayList<Integer>();

然后在if语句中添加满足的数字:

eightyNines.add(n);

你可以用增强的for循环打印它们:

for(int s : eightyNines)
{
    System.out.println(s);
}

这些只是您可以执行此操作的两种方法。希望这有帮助!

答案 1 :(得分:1)

Seans方法的另一种方法是,声明一个名为is89y的微小的布尔数组,默认情况下,每个位置都是false。

在检查数字时,您可能首先查看数组,而不是计算每个值。但请注意,您测试的最高数字可能会导致中间值,超过数组。选择更大的阵列可以部分避免这种情况。但是 - 检查。

类似的方法是使用Hashmap(int→Boolean)。

由于数字不是按顺序计算的,所以你应该解决这个递归问题,这样每个中间数都可以存储在数组/ hashmap中。速度的提升应该是巨大的。

准备好后,您可以在数组中查找真实位置。

Scala中的概念证明*:

def qdz (MAX: Int): Seq [Int] = {

    val arr = new Array [Boolean](MAX*3)

    def qdsum (n: Int) : Int = {
        if (n < 10) n*n else
        (n % 10)*(n % 10) + qdsum (n/10)
    }

    def isa89 (n: Int): Boolean = {
        if (n < MAX*3 && arr(n)) true
        else {
            val sum = qdsum (n)
            // if (sum > MAX) print (s" <<$sum>> ")
            if (sum == 1)  false else
            if (sum == 89) {
                if (n < MAX*3) arr(n) = true
                if (sum < MAX*3) arr(sum) = true
                true
            }
            else {
                val tmp = isa89 (sum)
                if (tmp && n < MAX*3) arr(n) = true
                tmp
            }
        }
    }
    val res = (1 to MAX).map (i=> isa89 (i)).filter (a => a)
    println (res.length)
    val nums= (0 to MAX).filter (i => arr(i))
    nums.foreach (i => print (i + " "))
    nums
}
val hits = qdz (100)

结果:

hits: Seq[Int] = Vector(2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15, 16, 17, 18, 20, 21, 22, 24, 25, 26, 27, 29, 30, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 83, 84, 85, 87, 88, 89, 90, 92, 93, 95, 96, 98, 99)

*)对于Java-Dev,理解中可能存在的最大障碍可能是,Scala中的arr(n)与Java中的arr [n]类似,并且缺少return语句。最后一个表达式都是return语句。所以:

    def qdsum (n: Int) : Int = {
        if (n < 10) n*n else
        (n % 10)*(n % 10) + qdsum (n/10)
    }

在Java中看起来像这样:

    int qdsum (Int n) {
        if (n < 10) return n*n;
        else return (n % 10)*(n % 10) + qdsum (n/10);
    }

    def isa89 (n: Int): Boolean = {
        if (n < MAX*3 && arr(n)) true // ...

获取

    boolean isa89 (int n) {
        if (n < MAX*3 && arr[n]) return true; // ...

答案 2 :(得分:0)

在您的代码中,您只需尝试一个起始号码,然后计算您在&#34;起始号码&#34;中达到89的次数。脚步。如果序列达到初始数的1或约1/8,如果达到89(因为循环长度为8),则该值为0。

我想你想要这样的东西

public static void main(String[] args)  {
    count89(500);
    count89(1000);
    count89(1221);
}

public static void count89(int max) {
    int count = 0;
    for (int i=1; i<max; i++) {
        if (endIn89(i)) count++;
    }
    System.out.println("f(" + max + ") = " + count);
}

public static boolean endIn89(int n) {
    while (true) {
        int next = 0;
        int digit = 0;
        while (n >= 1) {
            digit = n % 10;
            next += digit * digit;
            n = n / 10;
        }
        n = next;
        if (n==1) return false;
        if (n==89) return true;
    }
}