首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >递归求解A^4 + B^4 + C^4 + D^4 = E^4

递归求解A^4 + B^4 + C^4 + D^4 = E^4
EN

Stack Overflow用户
提问于 2014-08-24 10:50:40
回答 2查看 2.4K关注 0票数 0

你如何写一个算法来找出哪些方格组合相等?

Asq + bsq + csq + dsq = esq

1+4+9+16=25假过大

1+4+9+16=36假太小

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-24 17:53:04

如果您想使用递归,那么首先应该创建一个方法,该方法:

  1. 将整数列表(Long)作为输入,
  2. 检查输入是否与条件匹配。
  3. 返回与等式匹配的
  4. 否则会增加一个输入值。
  5. 以更高的价值呼叫自己。

所以看起来是这样:

代码语言:javascript
运行
复制
private void trySolutionRecursivly(final Long[] values) 
{
    System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    if (conditionMet(values))
    {
        System.out.println("Met the condition!");
    }
    else
    {
        Long[] newValues = increaseValues(values);
        trySolutionRecursivly(newValues);
    }   
}

该方法的第一个调用如下所示(一个包含5个数组的数组):

代码语言:javascript
运行
复制
trySolutionRecursivly({1L, 1L, 1L, 1L, 1L});

但是如果您尝试递归地执行这个操作,您将得到StackOverflowError,因为组合太多了,递归调用也太多了(我已经尝试过了)。因此,唯一的解决方案是在循环中依次调用该方法,例如:

代码语言:javascript
运行
复制
private void findSolution()
{
    Long[] newValues = {1L, 1L, 1L, 1L, 1L};
    while (conditionNotMet(newValues))
    {
        newValues = increaseValues(values);
    }
    System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
}

现在的诀窍是,正确地增加值。

您的问题是结合重复,这意味着存在(k+n-1)!/(k!(n-1)!),其中k=5n=400(不能是100;),所以在我们的例子中,its:(5+400-1)!/(5!(399)!)=404!/(5!399!),这是400*401*402*403*404/120=87485400080可能的解决方案。这是相当多的,这就是为什么递归不能在这里工作(在最坏的情况下,程序必须存储大约87485400080个方法调用的信息)。

现在,结合重复的工作如下,4个价值和3个地方:

代码语言:javascript
运行
复制
1;1;1
1;1;2
1;1;3
1;1;4
1;2;2
1;2;3
1;2;4
1;3;3
1;3;4
1;4;4
2;2;2
2;2;3
2;2;4
2;3;3
2;3;4
2;4;4
3;3;3
3;3;4
3;4;4
4;4;4

您可以注意到,每次最后一个索引达到4(最大值)时,第二个到最后一个索引将增加一个,最后一个索引将设置为与第二个索引相同的值。所以这个实现应该是这样的:

代码语言:javascript
运行
复制
private Long[] increaseValues(final Long[] values) 
{
    boolean reindexed = false;
    for (int i = 0; i < values.length; i++)
    {
        if (values[i] == MAX_VALUE)
        {
            if (i > 0)
            {
                values[i-1]++;
                reindex(i, values);
                reindexed = true;
                break;
            }
            else
            {
                throw new IllegalStateException("No solution found.");
            }
        }
    }
    if (!reindexed)
    {
        values[values.length-1]++;
    }
    return values;
}

private Long[] reindex(final Integer startIndex, final Long[] values)
{
    Long startingValue = values[startIndex - 1];
    for (int i = startIndex; i < values.length; i++)
    {
        values[i] = startingValue;
    }
    return values;
}

扰流板警报

总之,这段代码会起作用,并给出答案。如果您想尝试并查看会发生什么,也会有注释过的递归代码。

代码语言:javascript
运行
复制
public class Main 
{
    final static int MAX_VALUE = 400;
    final static Long[] powers = new Long[MAX_VALUE + 1];

    public static void main(String args[])
    {
        final Long[] values = {1L, 1L, 1L, 1L, 1L};


        for (Integer i = 0; i <= MAX_VALUE; i++)
        {
            powers[i.intValue()] = Double.valueOf(Math.pow(i.doubleValue(), 4d)).longValue();
        }
        //new Main().trySolutionRecursivly(values);
        new Main().findSolution(values);
    }

    private void findSolution(final Long[] values)
    {
        Long[] newValues = values;
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }

    private boolean conditionNotMet(final Long[] values)
    {
        return !conditionMet(values);
    }

    private boolean conditionMet(final Long[] values)
    {
        return pow(values[0]) + pow(values[1]) + pow(values[2]) + pow(values[3]) == pow(values[4]); 
    }

    private Long pow(final Long value)
    {
        return powers[value.intValue()];
        //return Math.pow(value.doubleValue(), 4d);
    }

    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }

    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
}

扰流板

输出(得到它需要一段时间-我花了大约15分钟):

代码语言:javascript
运行
复制
a = 30; b = 120; c = 272; d = 315; e = 353

这是证明,它是正确的。

,PS,,这实际上是一个很好的想法--在数组中存储功率值。在有这么多循环的情况下,它确实产生了不同的效果。此外,不要试图打印每一种情况--它会大大降低程序的速度。

票数 2
EN

Stack Overflow用户

发布于 2014-08-24 11:28:15

不要用一个来提高所有的值;相反,把ABCD当作基数为100的阿拉伯数字(“位”是整数1-100,1是你的“零”数字),并编写生成所有这些数字的代码。基本上,

  1. 设定A.B.C.D为1.1.1.1;
  2. 计算和,在数组中搜索匹配;
  3. 增量D;
  4. 如果D大于100,则重置为1并增加C;
  5. 对B重复步骤3-4,然后是A;
  6. 当溢出时停止。

并且,停止对整数算术使用doubles,而使用longs。double只能精确地表示最多2^54 (或类似的)整数,而long可以使您达到2^64。除了范围之外,使用double还可以打开虚假否定的大门(有解决方案的ABCD的组合,但您的代码错过了它)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25470920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档