同余数


21

定义:

  • 如果一个内角正好是90度,则将三角形视为直角三角形
  • 如果数字可以由整数的比率表示,则该数字被认为是有理数,即和p/q,其中p和和q均为整数。
  • 如果存在一个三边均是有理的面积的直角三角形,则数字n全等数字n
  • 这是OEIS A003273

挑战

这是一个挑战。给定输入数字时,x如果x是全等数字,则输出一个不同且一致的值,如果不是一个全等数字,则输出一个单独的不同且一致的值x。输出值不一定需要使用您的语言为真/假。

特殊规则

出于此挑战的目的,您可以假设Birch和Swinnerton-Dyer猜想是正确的。另外,如果您可以证明Birch和Swinnerton-Dyer的猜想,请索取您的$ 1,000,000千禧年奖金。;-)

例子

(使用True全等数字False)。

5 True
6 True
108 False

规则和说明

  • 输入和输出可以通过任何方便的方法给出。
  • 您可以将结果打印到STDOUT或将其作为函数结果返回。请在您的提交中说明输出可以采用的值。
  • 完整的程序或功能都是可以接受的。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

3
输入是正整数吗?
林恩

我最初的方法是将输入乘以任意平方数,直到输入为毕达哥拉斯三边形的腿乘积的一半,但是后来我意识到,为非一致输入而实际上终止可能有点困难。
不相关的字符串

@西安好的,但是挑战应该是独立的。
Lynn

@Lynn是,输入将为正整数。
AdmBorkBork

Answers:


8

R,179 173 142 141 137 135 134字节

使用基于Tunnell定理的相同参数,返回0if if nnot congruent and 1else。(花了我很长时间才发现仅适用于无平方整数的条件的约束。)

function(n){b=(-n:n)^2
for(i in b^!!b)n=n/i^(!n%%i)
P=1+n%%2
o=outer
!sum(!o(y<-o(8/P*b,2*b,"+")/P-n,z<-16/P*b,"+"),-2*!o(y,4*z,"+"))}

在线尝试

ArnaudGiuseppe带来的改进(最终代码大部分是Guiseppe的!),Robin有了-3

语法分析:

for(i in b[b>0])n=n/i^(!n%%i) #eliminates all square divisors of n
P=2^(n%%2)                    #n odd (2) or even (1)
o=outer                       #saves 3 bytes 
o(8/P*b,2*b,"+")/P-n          #all sums of (8/P)x^2+(2/P)*y^2-n
o(...,16/P*b,"+")             #all sums of above and (16/P)*z^2
o(...,4*z,"+"))               #all sums of above and (64/P)*z^2
!o(...,4*z,"+"))              #all sums of above equal to zero
!sum(!...,2*!...)             #are zeroes twice one another (Tunnell)

Tunnell定理指出,n是一致的,当且仅当整数解的要数2x²+ y 2 +8z²= n是两倍整数解到2x²数+ y 2 +32z²= N,如果n是奇数和数量如果n为偶数,则8x²+y²+16z²= n的整数解的数量是8x²+y²+64z²= n的整数解的数量的两倍。


1
欢迎来到PPCG!目标是使代码尽可能短。也许您可能会看这些打高尔夫球的技巧或这些R特定的技巧
朱塞佩

1
有很多空白,我也建议您提供一个链接,以尝试在线!帮助验证您的代码:-)
朱塞佩

1
如果您愿意,也可以随时与R高尔夫球手聊天。您可以通过使用@[username]... 进行通知...我猜您是Robin Ryder吸引您参加代码高尔夫的吗?
朱塞佩

1
142个字节 -匿名函数是完全没有的,我做了一些其他的了高尔夫,我很高兴来解释
朱塞佩

1
真好!您有使用理由-n:n吗?我没看过隧道定理,但在我看来,n:0对于-1字节同样适用。用于复制和粘贴到PPCG的格式:-)编辑:我知道,在某些情况下n:0不起作用。
朱塞佩

3

锈-282字节

fn is(mut n:i64)->bool{let(mut v,p)=(vec![0;4],n as usize%2);while let Some(l)=(2..n).filter(|i|n%(i*i)==0).nth(0){n/=l*l;}for x in -n..=n{for y in -n..=n{for z in -n..=n{for i in 0..2{if n-6*x*x*(n+1)%2==2*x*x+(2-n%2)*(y*y+(24*i as i64+8)*z*z){v[2*p+i]+=1};}}}}v[2*p]==2*v[2*p+1]}
  • 使用Jerrold B. Tunnell定理,该定理我实际上并不理解,但无论如何似乎仍然有效。
  • 将n除以所有平方因子,使其成为“平方自由”,因为在下面的论文中,Tunnel定理仅针对平方自由进行了描述。
    • 我相信这可能行得通,因为每个全等数乘以一个平方会创建一个更大的全等数,反之亦然。因此,通过测试较小的数字,我们可以验证较大的数字,在本例中为n。(所有删除的正方形,都可以相乘成一个大正方形)。
  • 遍历x,y,z整数的所有可能组合,测试Tunnell方程:
    if n is odd, test if n = 2x2+y2+32z2 and/or 2x2+y2+8z2
    if n is even, test if n = 8x2+2y2+64z2 and/or 8x2+2y2+16z2
    • 在代码本身中,四个方程在循环内被压缩成一个方程,对偶数/奇数使用模
  • 记下哪些方程式与n相符
  • 循环后,测试计数的比率(每个Tunnell)

也可以看看:

已校正偶/奇,谢谢 @Level River St


1
哦好吧,在我开始工作的时候,我只看到了c ++的答案,这是错误的...
唐明亮

感谢级别河圣
穿上鲜艳的

3

C ++(gcc)251234字节

感谢@arnauld指出我的愚蠢错字。

-17个字节感谢@ceilingcat。

#import<cmath>
int a(int n){int s=sqrt(n),c,x=-s,y,z,i=1,X;for(;++i<n;)for(;n%(i*i)<1;n/=i*i);for(;x++<s;)for(y=-s;y++<s;)for(z=-s;z++<s;c+=n&1?2*(n==X+24*z*z)-(n==X):2*(n==4*x*x+2*X+48*z*z)-(n/2==2*x*x+X))X=2*x*x+y*y+8*z*z;return!c;}

在线尝试!

如果n相等则返回1 ,否则返回0。

qs2q 也是一致的(算法似乎在某些包含平方的数字上中断。


1
@Arnauld:啊,那是我的错字。固定。
尼尔·A。

1

JavaScript(ES7),165个字节

就像@NeilA。的答案一样,它基于Tunnell 定理,因此假设Birch和Swinnerton-Dyer猜想是正确的。

返回一个布尔值。

n=>(r=(g=i=>i<n?g(i+!(n%i**2?0:n/=i*i)):n**.5|0)(s=2),g=(C,k=r)=>k+r&&g(C,k-1,C(k*k)))(x=>g(y=>g(z=>s+=2*(n==(X=(n&1?2:8)*x+(o=2-n%2)*y)+o*32*z)-(n==X+o*8*z))))|s==2

在线尝试!

怎么样?

nnr=ns2

r = (                // we will eventually save isqrt(n) into r
  g = i =>           // g = recursive function taking an integer i
    i < n ?          //   if i is less than n:
      g(i + !(       //     do a recursive call with either i or i + 1
        n % i**2 ?   //     if n is not divisible by i²:
          0          //       yield 0 and therefore increment i
        :            //     else:
          n /= i * i //       divide n by i² and leave i unchanged
      ))             //     end of recursive call
    :                //   else:
      n ** .5 | 0    //     stop recursion and return isqrt(n)
  )(s = 2)           // initial call to g with i = s = 2

gCk2r<kr.

  g = (C, k = r) =>  // C = callback function, k = counter initialized to r
    k + r &&         //   if k is not equal to -r:
    g(               //     do a recursive call:
      C,             //       pass the callback function unchanged
      k - 1,         //       decrement k
      C(k * k)       //       invoke the callback function with k²
    )                //     end of recursive call

We finally use 3 nested calls to g to walk through all triplets (x,y,z)[r+1,r]3 and update s to test whether 2An=Bn if n is odd or 2Cn=Dn if n is even, with:

An=#{(x,y,z)[r+1,r]3n=2x2+y2+32z2}Bn=#{(x,y,z)[r+1,r]3n=2x2+y2+8z2}Cn=#{(x,y,z)[r+1,r]3n=8x2+2y2+64z2}Dn=#{(x,y,z)[r+1,r]3n=8x2+2y2+16z2}

g(x =>                            // for each x:      \    NB:
  g(y =>                          //   for each y:     >-- all these values are
    g(z =>                        //     for each z:  /    already squared by g
      s +=                        //       add to s:
        2 * (                     //         +2 if:
          n == (                  //           n is equal to either
            X =                   //           An if n is odd (o = 1)
            (n & 1 ? 2 : 8) * x + //           or Cn if n is even (o = 2)
            (o = 2 - n % 2) * y   //
          ) + o * 32 * z          //
        ) - (                     //         -1 if:
          n == X + o * 8 * z      //           n is equal to either
        )                         //           Bn if n is odd
    )                             //           or Dn if n is even
  )                               //
)                                 // if s in unchanged, then n is (assumed to be) congruent

1

Ruby, 126 bytes

->n{[8,32].product(*[(-n..-t=1).map{|i|i*=i;n%i<1&&n/=i;i}*2+[0]]*3).map{|j|d=2-n%2
k,x,y,z=j
2*d*x+y+k*z==n/d&&t+=k-16}
t==1}

Try it online!

found a place to initialize t=1 and expanded the list of squares into a triplet instead of using q to make additional copies.

Ruby, 129 bytes

->n{t=0
[8,32].product(q=(-n..-1).map{|i|i*=i;n%i<1&&n/=i;i}*2+[0],q,q).map{|j|d=2-n%2
k,x,y,z=j
2*d*x+y+k*z==n/d&&t+=k-16}
t==0}

Try it online!

Uses Tunnell's theorem like the other answers. I use a single equation as follows.

2*d*x^2 + y^2 + k*z^2 == n/d  where d=2 for even n and d=1 for odd n

We check the cases k=8 and k=32 and check if there are twice as many solutions for k=8 than k=32. This is done by adding k-16 to t every time we find a solution. This is either +16 in the case k=32 or -8 in the case k=8. Overall the number is congruent if t is the same as its initial value at the end of the function.

It is necessary to find all solutions to the test equation. I see many answers testing between +/-sqrt n. It is perfectly OK to test also outside these limits if it makes code shorter, but no solutions will be found because the left side of the equation will exceed n. The thing I missed in the beginning is that negative and positive x,y,z are considered separately. Thus -3,0,3 yields three squares 9,0,9 and all solutions must be counted separately (0 must be counted once and 9 must be counted twice.)

Ungolfed code

->n{t=0                              #counter for solutions

  q=(-n..-1).map{|i|i*=i;n%i<1&&n/=i #make n square free by dividing by -n^2 to -1^2 as necessary 
  i}*2+[0]                           #return an array of squares, duplicate for 1^2 to n^2, and add the case 0 

  [8,32].product(q,q,q).map{|j|      #make a cartesian product of all possible values for k,x,y,z and iterate
    d=2-n%2                          #d=1 for odd n, 2 for even n
    k,x,y,z=j                        #unpack j. k=8,32. x,y,z are the squared values from q.
    2*d*x+y+k*z==n/d&&t+=k-16}       #test if the current values of k,x,y,z are a valid solution. If so, adjust t by k-16 as explained above.
t==0}                                #return true if t is the same as its initial value. otherwise false.

about positive and negative solutions, same here, I wasted quite a while missing this point!
Xi'an
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.