我需要将多少个平方,立方,四次幂等求和为n?


14

您会得到一个非负整数n和一个integer p >= 2。您需要将一些次p幂(p=2即正方形,p=3表示立方体)加在一起才能得到n。这始终适用于任何非负数n,但您不知道需要多少次p幂(任何整数)。

这是您的任务:找到p可以求和的最小n 次方n

例子

>>> min_powers(7, 2)
4                       # you need at least four squares to add to 7
                        # Example: (2)^2 + (1)^2 + (1)^2 + (1)^2 = 4 + 1 + 1 + 1 = 7
>>> min_powers(4, 2)
1                       # you need at least one square to add to 4
                        # Example: (2)^2 = 4
>>> min_powers(7, 3)
7                       # you need at least seven cubes to add to 7
                        # Example: 7*(1)^3 = 7
>>> min_powers(23, 3)
9                       # you need at least nine cubes to add to 23
                        # Example: 2*(2)^3 + 7*(1)^2 = 2*8 + 7*1 = 23

有关此问题Waring问题的相关Wikipedia文章。

规则

  • 您的代码必须是程序或函数。

  • 输入是两个整数,n并且可以p按任意顺序输入。您可以假设所有输入均有效(n是任何正整数,p >= 2

  • 输出是一个整数,表示求和所需的幂数n

  • 这是代码高尔夫,所以最短的程序获胜,不一定是最有效的。

  • 允许所有内置插件。

与往常一样,如果问题仍然不清楚,请告诉我。祝你好运,打高尔夫球!


好吧,看起来蛮力会赢。我希望不是。
lirtosiast,2015年

3
这个问题是令人难以置信的努力,我怀疑任何回答要么不能完成,同时给予正确的结果。
orlp 2015年

至少有上限
qwr 2015年

Answers:


5

Pyth,20个 19字节

感谢FryAmTheEggman,节省了1个字节

L&bhSmhy-b^dQS@bQyE

p首先输入两行,然后输入n

在线尝试。 测试套件。

说明

该代码定义了一个递归函数y(b),该函数返回的结果min_powers(b, p)

L                      define a function y(b):
 &b                      return b if it's 0
             S           get a list of positive integers less than or equal to
              @bQ        the p:th root of b
     m                   map the integers to:
        -b                 subtract from b
          ^dQ              the p:th power of the current integer
       y                   recurse on the above
      h                    increment the result
    hS                   find the smallest result number and return it
                 yE    calculate y(n) and print

8

Mathematica 61 50字节

由LegionMammal978保存的11个字节。

当限于对数字进行计数时,此问题很简单(在Mathematica中)。当扩展为包括整数的幂时,这是一场噩梦。

(k=0;While[PowersRepresentations[#,++k,#2]=={}];k)&

测试用例

(k = 0; While[PowersRepresentations[#, ++k, #2] == {}]; k) &[7, 2]
(k = 0; While[PowersRepresentations[#, ++k, #2] == {}]; k) &[4, 2]
(k = 0; While[PowersRepresentations[#, ++k, #2] == {}]; k) &[7, 3]
(k = 0; While[PowersRepresentations[#, ++k, #2] == {}]; k) &[23, 3]

4

1个

7

9


PowersRepresentationsp[n,k,p]查找所有n可以表示为k升至p-次幂的正整数之和的情况。


例如,

PowersRepresentations[1729, 2, 3]

{{1,12},{9,10}}

检查,

1^3 + 12^3

1729


9^3 + 10^3

1729


像Mathematica这样的具有竞争力的语言无法达到这些目的……不需要任何创造力就可以知道函数名称。但是仍然写得很好。
csga5000

1
@ csga5000嘿,高尔夫语言赢得了本网站上99%的挑战……
LegionMammal978

@ LegionMammal978虽然我不csga的观点一致,高尔夫东西倒在打高尔夫球语言需要巨大的创造力量。
门把手

2
同意,此提交不授予任何创新奖。也不紧凑:Pyth提交的长度不到一半。当像Mathematica这样的语言可以被重铸为更普遍的现象的实例并且高级功能的异常组合可以发挥作用时,问题就变得具有挑战性。他们也变得更加有趣。
DavidC

3

Java- 183177字节

int p(int a,int b){int P,c,t,l=P=t=a,f=0;double p;while(P>0){a=t=l;c=0;while(t>0){if(a-(p=Math.pow(t,b))>=0&&t<=P){while((a-=p)>=0)c++;a+=p;}t--;}f=c<f||f==0?c:f;P--;}return f;}

183字节

int p(int a,int b){int P,c,t,l,f=0;P=t=l=a;double p;while(P>0){a=t=l;c=0;while(t>0){if(a-(p=Math.pow(t,b))>=0&&t<=P){while((a-=p)>=0){c++;}a+=p;}t--;}f=c<f||f==0?c:f;P--;}return f;}

不打高尔夫球

int p(int a, int b){
    int P,c,t,l=P=t=a,f=0;
    double p;
    while (P>0){
        a=t=l;
        c=0;
        while (t>0){
            if (a-(p=Math.pow(t, b))>=0 && t<=P){
                while((a-=p)>=0)c++;
                a+=p;
            }
            t--;
        }
        f=c<f||f==0?c:f;
        P--;
    }
    return f;
}

结果

System.out.println(p(7, 2));    // 4
System.out.println(p(4,2));     // 1
System.out.println(p(7,3));     // 7
System.out.println(p(23,3));    // 9

该答案无效。p(32,2)返回5时,它应该返回24^2 + 4^2 = 32)。
PurkkaKoodari

@ Pietu1998好,我将对其进行修改。
Yassin Hajaj

@ Pietu1998你会怎么做?
Yassin Hajaj 2015年

我以递归方式进行操作,检查每个数字的每种可能功效。
PurkkaKoodari 2015年

1
@YassinHajaj为Java +1并自己做
csga5000

1

Python 2,66个字节

f=lambda n,p:n and-~min(f(n-k**p,p)for k in range(1,n+1)if n/k**p)

递归地尝试减去p使余数为非负数的第n次幂,然后计算每个余数的值,并取最小值加1。在0上,输出0。

丑陋的检查if n/k**p(相当于if k**p<=n)是要阻止函数进入负值并尝试获取min空列表的。如果Python具有min([])=infinity,则不需要。


哇。这比我在Python上的测试代码短很多。+1!
Sherlock9年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.