奇数平方和小于N


19

写一个程序或函数输出的总和奇数平方数(OEIS#A016754)小于一个输入 n

序列中的前44个数字是:

1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961, 1089, 
1225, 1369, 1521, 1681, 1849, 2025, 2209, 2401, 2601, 2809, 3025, 3249, 3481, 
3721, 3969, 4225, 4489, 4761, 5041, 5329, 5625, 5929, 6241, 6561, 6889, 7225, 7569

序列的公式为a(n) = ( 2n + 1 ) ^ 2

笔记

  • 您的程序的行为可能未定义n < 1(即所有有效输入均为)>= 1

测试用例

1 => 0
2 => 1
9 => 1
10 => 10
9801 => 156849
9802 => 166650
10000 => 166650

1
对此,没有任何亲密的理由是解决挑战的正当理由……
Mego

Answers:


22

果冻,6个字节

½Ċ|1c3

在线尝试!验证所有测试用例

背景

对于所有正整数k,我们有1²+3²+⋯+(2k-1)²= k(2k-1)(2k +1)÷3

由于有m C r = m!÷(((mr)!r!) r-一组m个元素的组合,上式可以计算为(2k +1)C 3 =(2k +1)2k(2k-1)÷6 = k(2k -1)(2k +1)÷3。

要应用该公式,我们必须找到最高的2k + 1,使得(2k-1)²<n。暂时忽略奇偶校验,我们可以计算出最高的m,使得(m-1)²<n等于m = ceil(srqt(n))。要有条件地增加m(如果它是偶数),只需计算m |。1(按位或与1)。

怎么运行的

½Ċ|1c3  Main link. Argument: n

½       Compute the square root of n.
 Ċ      Round it up to the nearest integer.
  |1    Bitwise OR with 1 to get an odd number.
    c3  Compute (2k + 1) C 3 (combinations).

6

JavaScript(ES6),30个字节

f=(n,i=1)=>n>i*i&&i*i+f(n,i+2)

31个字节(如果f(1)需要返回零而不是false):

f=(n,i=1)=>n>i*i?i*i+f(n,i+2):0

6

05AB1E10 8字节

码:

<tLDÉÏnO

说明:

<         # Decrease by 1, giving a non-inclusive range.
 t        # Take the square root of the implicit input.
  L       # Generate a list from [1 ... sqrt(input - 1)].
   DÉÏ    # Keep the uneven integers of the list.
      n   # Square them all.
       O  # Take the sum of the list and print implicitly.

可能派上用场:t;L·<nO

使用CP-1252编码。在线尝试!


6

Haskell,30个字节

f n=sum[x^2|x<-[1,3..n],x^2<n]

令人惊讶的正常外观。


4

C#,126个 131字节

编辑版本以符合新问题:

class P{static void Main(){int x,s=0,n=int.Parse(System.Console.ReadLine());for(x=1;x*x<n;x+=2)s+=x*x;System.Console.Write(s);}}

使用硬编码限制:

using System;namespace F{class P{static void Main(){int x,s=0;for(x=1;x<100;x+=2)s+=x*x;Console.Write(s);Console.Read();}}}

4
欢迎来到编程难题和Code Golf!此处,答案标头的商定格式是# Language name, number bytes为了保持一致。

2
你为什么Console.Read最后呢?
Martin Ender

1
namespace单个文件不需要。
ASCII码,仅ASCII

1
如果还可以System.Console.Write(s);,并且不需要,您还应该能够保存一些字节Console.Read
仅ASCII码

2
@Thomas您可以在VS中使用Ctrl + F5来运行程序,在这种情况下,程序终止后窗口将保持打开状态。
Martin Ender

4

果冻,7岁

’½R²m2S

在线尝试或尝试多个值的修改版本

嘘...丹尼斯在睡觉...

感谢Sp3000在聊天中的帮助!

说明:

’½R²m2S
’           ##  Decrement to prevent off-by-one errors
 ½R²        ##  Square root, then floor and make a 1-indexed range, then square each value
    m2      ##  Take every other value, starting with the first
      S     ##  sum the result

9
丹尼斯实际上是醒着的。
丹尼斯

@丹尼斯啊!而且显然
也要


4

R,38 36字节

function(n,x=(2*0:n+1)^2)sum(x[x<n])

@Giuseppe通过移动节省了两个字节 x参数列表以保存花括号来节省了。好主意!

不打高尔夫球

function(n, x = (2*(0:n) + 1)^2)  # enough odd squares (actually too many)
  sum(x[x < n])                   # subset on those small enough
}

在线尝试!


2
欢迎来到PPCG!
Martin Ender '18

这个网站很棒,谢谢!
Michael M

您应该能够通过移入x默认函数参数来节省两个字节,然后可以删除花括号。
朱塞佩

3

C,51、50 48字节

f(n,s,i)int*s;{for(*s=0,i=1;i*i<n;i+=2)*s+=i*i;}

因为为什么不使用最冗长的语言之一打高尔夫球呢?(嘿,至少不是Java!)

在线尝试!

完整的无程序程序,带有测试I / O:

int main()
{
    int s;
    f(10, &s);
    printf("%d\n", s);
    char *foobar[1];
    gets(foobar);
}

f(n,s,i)int*s;{for(*s=0,i=1;i*i<n;i+=2)*s+=i*i;}

most verbose languages更多golfy比Python,C#,LISP,福斯等,C实际上是高尔夫相当不错的

@cat我不认为它比python更像高尔夫球。它绝对比java,rust和C#更好,但是此挑战的每个python答案都是< 50 bytes。此外,还有一个相关的元后这里
DJMcMayhem

3

实际上是7个字节

√K1|3@█

在线尝试!

同样为7个字节:

3,√K1|█

在线尝试!

它使用与Dennis Jelly答案相同的公式。

说明:

√K1|3@█
√K       push ceil(sqrt(n))
  1|     bitwise-OR with 1
    3@█  x C 3

下一个会叫Literally吗?

3

八度,23字节

@(x)(x=1:2:(x-1)^.5)*x'

测试:

[f(1); f(2); f(3); f(10); f(9801); f(9802); f(10000)]
ans =    
        0
        1
        1
       10
   156849
   166650
   166650

3

CJam,15字节

qi(mq,2%:)2f#1b

在线尝试!

硬编码的10000个解决方案:

马丁的12字节解决方案:

99,2%:)2f#1b

我原来的13字节解决方案:

50,{2*)2#}%:+

在线尝试!


您的代码为14字节(链接中有尾随换行符),但我认为它对于输入9801不正确,因为挑战要求的平方小于输入。
Martin Ender

@MartinButtner是的,你是对的。我会看看我是否能找到一个不错的解决方法
A Simmons

2

Pyth,10个字节

s<#Qm^hyd2

测试套件

说明:

s<#Qm^hyd2
    m          Map over the range of input (0 ... input - 1)
       yd      Double the number
      h        Add 1
     ^   2     Square it
 <#            Filter the resulting list on being less than
   Q           The input
s              Add up what's left

备用(10个字节):s<#Q%2t^R2
Leaky Nun

2

Mathcad,31个“字节”

enter image description here

请注意,Mathcad使用键盘快捷键输入多个运算符,包括定义和所有编程运算符。例如,ctl-]进入while循环-无法键入,只能使用键盘快捷键或从“编程”工具栏输入。“字节”被视为输入Mathcad项(例如,变量名或运算符)所需的键盘操作数。

由于我没有赢得比赛的机会,所以我想我可以使用直接公式版本添加一些变化。


MathCAD如何评分?我在哪里可以买到?

得分你给的解释是有点......脆弱,IMO

1
您需要针对这种语言的评分提出一个元问题。
Mego 2016年

元问题听起来不错。试图不计较分的得分很快就会变成战争与和平。
Stuart Bruff

2

球拍,57字节

(λ(n)(for/sum([m(map sqr(range 1 n 2))]#:when(< m n))m))

2

MATL,10字节

qX^:9L)2^s

编辑(2016年7月30日):链接的代码替换9L1L以适应语言的最新更改。

在线尝试!

q    % Implicit input. Subtract 1
X^   % Square root
:    % Inclusive range from 1 to that
9L)  % Keep odd-indexed values only
2^   % Square
s    % Sum of array

1

Python,39个字节

f=lambda n,i=1:+(i*i<n)and i*i+f(n,i+2)

如果,对于n=1,有效的是输出False而不是0,那么我们可以避免将基本情况转换为获取37个字节

f=lambda n,i=1:i*i<n and i*i+f(n,i+2)

这是奇怪的是,我还没有找到一个更短的方式来获得0i*i>=n非零值,否则。在Python 2中,使用

f=lambda n,i=1:~-n/i/i and i*i+f(n,i+2)

boolintPython中的子类,这False是的可接受值0



1

Python 2,38个字节

s=(1-input()**.5)//2*2;print(s-s**3)/6

根据Dennis的公式,带有s==-2*k。输出浮点数。实际上,输入是平方根,递减,然后四舍五入到下一个偶数。


1

PARI / GP 33 32 26字节

改编自 Dennis的代码

n->t=(1-n^.5)\2*2;(t-t^3)/6

我的第一个想法(30字节),使用一个简单的多项式公式:

n->t=((n-1)^.5+1)\2;(4*t^3-t)/3

这是一个有效的实现,实际上与我要编写的非高尔夫版本没有太大区别:

a(n)=
{
  my(t=ceil(sqrtint(n-1)/2));
  t*(4*t^2-1)/3;
}

遍历每个正方形的替代实现(37字节):

n->s=0;t=1;while(t^2<n,s+=t^2;t+=2);s

另一个替代解决方案(35字节),演示没有临时变量的求和:

n->sum(k=1,((n-1)^.5+1)\2,(2*k-1)^2)

使用L 2范数的另一种解决方案,不是特别有竞争力(40字节)。如果支持具有步长索引的向量,则更好。(可以想象语法n->norml2([1..((n-1)^.5+1)\2..2])会丢失8个字节。)

n->norml2(vector(((n-1)^.5+1)\2,k,2*k-1))

1

Haskell,32 31字节

 n#x=sum[x^2+n#(x+2)|x^2<n]
 (#1)

用法示例:(#1) 9802-> 166650

编辑:@xnor保存了一个字节,并具有聪明的列表理解功能。谢谢!


欺骗后卫要短一个字节:n#x=sum[x^2+n#(x+2)|x^2<n]
xnor

1

朱莉娅,29个字节

f(n,i=1)=i^2<n?i^2+f(n,i+2):0

这是一个接受整数并返回整数的递归函数。

我们从1开始索引,如果它的平方小于输入,则取平方并在索引+ 2上加上reuse的结果,这确保跳过偶数,否则返回0。


1

Oracle SQL 11.2,97字节

SELECT NVL(SUM(v),0)FROM(SELECT POWER((LEVEL-1)*2+1,2)v FROM DUAL CONNECT BY LEVEL<:1)WHERE v<:1;

1

朱莉娅,26个字节

x->sum((r=1:2:x-1)∩r.^2)

这将构造所有低于n的奇数正整数的范围以及该范围内整数的平方数组,然后计算两个可迭代变量中整数的总和。

在线尝试!


1

Reng v.3.3,36个字节

0#ci#m1ø>$a+¡n~
:m%:1,eq^c2*1+²c1+#c

在这里尝试!

说明

1:初始化

 0#ci#m1ø

设置c0(计数器),输入Im斧头。转到下一行。

2:循环

:m%:1,eq^c2*1+²c1+#c

:复制当前值(平方的奇数),然后[我m放下m斧头。我在另一个答案中使用了小于技巧,在这里使用了它。%:1,e检查STOS <TOS。如果是,q^则上升并跳出循环。除此以外:

         c2*1+²c1+#c

c将计数器放下,2*加倍,加1+一,然后²平方。c1+#C递增c,循环再次进行。

3:最终

        >$a+¡n~

$丢弃最后一个值(大于期望值),a+¡相加直到堆栈的长度为1,然后n~输出并终止。



1

Mathematica 30字节

Total[Range[1,Sqrt[#-1],2]^2]&

这个未命名的函数将所有小于输入(Range[1,Sqrt[#-1],2])的奇数平方并加起来。


1

PHP,64字节

function f($i){$a=0;for($k=-1;($k+=2)*$k<$i;$a+=$k*$k);echo $a;}

展开:

function f($i){
    $a=0;
    for($k=-1; ($k+=2)*$k<$i; $a+=$k*$k);
    echo $a;
}

在每个迭代for循环,这将增加2〜k和检查如果k 2是小于$i,如果是后添加K 2$a


1

R,60字节

function(n){i=s=0;while((2*i+1)^2<n){s=s+(2*i+1)^2;i=i+1};s}

完全按照质询中所述进行操作,包括在n = 1的情况下返回0。脱胶,';' 表示R中的换行符,在下面忽略:

function(n){         # Take input n
i = s = 0            # Declare integer and sum variables
while((2*i+1)^2 < n) # While the odd square is less than n
s = s + (2*i+1)^2    # Increase sum by odd square
i = i + 1            # Increase i by 1
s}                   # Return sum, end function expression

1

爪哇8,128个 119 117 111 49字节

n->{int s=0,i=1;for(;i*i<n;i+=2)s+=i*i;return s;}

基于@Thomas的C#解决方案

说明:

在线尝试。

n->{           // Method with integer as both parameter and return-type
  int s=0,     //  Sum, starting at 0
      i=1;     //  Index-integer, starting at 1
  for(;i*i<n;  //  Loop as long as the square of `i` is smaller than the input
      i+=2)    //    After every iteration, increase `i` by 2
    s+=i*i;    //   Increase the sum by the square of `i`
  return s;}   //  Return the result-sum

0

Python 2,49字节

最终比短lambda

x=input()
i=1;k=0
while i*i<x:k+=i*i;i+=2
print k

在线尝试

我最短lambda53个字节

lambda x:sum((n-~n)**2for n in range(x)if(n-~n)**2<x)
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.