方形如何结束?


20

在BASE-10,所有的完全平方结束01456,或9

在基地-16,所有完全平方结束014,或9

Nilknarf在这个答案中描述了这是为什么以及如何很好地解决这个问题,但是我还将在这里给出简短的描述:

当对以10为底的数字N进行平方运算时,“个”数字不受“十”个数字或“百”个数字等的影响。只有N中的“一位”数字会影响N 2中的“一位”数字,因此找到N 2的所有可能最后数字的一种简便(但可能不是最高尔夫)方法是为所有0 <= n找到n 2 mod 10 < 10。每个结果都是可能的最后一位数字。对于Base-m,您可以找到对于所有0 <= n < m n 2 mod m

编写一个程序,当输入为N时,输出以Base-N为单位的理想平方的所有可能的最后一位数字(无重复)。您可能会假设N大于0,并且N足够小以至于N 2不会溢出(如果可以一直测试到N 2,我会给您有限的布朗尼点,但您知道布朗尼点对实际点的汇率是无穷大)。

测试:

 Input -> Output
 1     -> 0
 2     -> 0,1
 10    -> 0,1,5,6,4,9
 16    -> 0,1,4,9
 31    -> 0,1,2,4,5,7,8,9,10,14,16,18,19,20,25,28
 120   -> 0,1,4,9,16,24,25,36,40,49,60,64,76,81,84,96,100,105

这是,因此适用标准规则!

(如果您觉得这太容易了,或者您希望在这个主题上有一个更深入的问题,请考虑以下问题:二次方残基测试的最小基数覆盖率)。


1
输出数组是否需要排序?
毛茸茸的

@蓬松的不!Mego,不允许重复。从理论上讲,N可能很大,因此重复将使输出几乎不可读。我会回答这个问题
法尔夸德勋爵(Lord Farquaad)'17

输出设置是否可以接受?
完全人类

2
@totallyhuman为什么无效?集是无序集合,因此不能排序,所以……
Xcoder先生17年

Answers:



19

Google表格,52 51 47字节

=ArrayFormula(Join(",",Unique(Mod(Row(A:A)^2,A1

由于泰勒·斯科特节省了4个字节

表格会自动在公式末尾添加4个右括号。

它不会按升序返回结果,但会返回正确的结果。

Results


神圣的牛,那人是杀人的杀手!谁会想到的?+1
bearacuda13

1
到目前为止,这绝对是我最喜欢的答案。
法夸德勋爵(Lord Farquaad)'17年

@LordFarquaad我很惊讶并很高兴收到如此好评。我一直在尝试在Sheets和Excel中进行更多的高尔夫运动,尽管-一部分是由于它们的范围如此有限。这导致了很多数组公式。
Engineer Toast

你应该能够删除终止)S对于-4字节
泰勒·斯科特

@TaylorScott谢谢!我最近在某个地方(可能是您的答案之一)看到了这个技巧,并且需要记住开始使用它。
Engineer Toast

6

05AB1E,5个字节

Lns%ê

在线尝试!或作为测试套件

L     # Range 1 .. input
 n    # Square each
  s%  # Mod by input
    ê # Uniquify (also sorts as a bonus)

s这里工作如何?输入是否重复?
路易斯·门多

@LuisMendo spop a,b; push b,a。当命令尝试从堆栈中弹出某些内容而没有剩余任何内容时,将使用下一个输入。如果没有更多输入,则使用最后一个输入(这里是一个示例)。在这种情况下,我本来可以使用¹它来推送第一个输入,但是s对于测试套件来说效果更好。
莱利

谢谢。您是否有关于重新使用输入的条件的更多信息?(如果有人说过三个输入,而您尝试从空堆栈中弹出两个值)?
路易斯·门多

1
@LuisMendo输入按顺序使用,直到用完为止,然后继续使用最后一个元素。您可以想象它就像是用每个输入顺序填充堆栈以及最后一个元素的无限数量填充一样。
莱利

@LuisMendo Ln¹%ê在这里等效。s
魔术章鱼缸

6

迅捷47 35 32 *字节

* -3感谢@Alexander。

斯威夫特的关系有史以来第一次击败Python吗?

{m in Set((0..<m).map{$0*$0%m})}

在线尝试!


说明

  • (0..<m).map{}-遍历范围[0...m)并映射以下结果:

  • $0*$0%m-每个以整数为底的整数的平方m

  • Set(...) -删除重复项。

  • m in -将基数分配给变量 m


用户名签出...请稍等。
Rohan Jhunjhunwala

1
更像击败了Python。真是令人印象深刻!我以为我永远也不会看到发生的那一天。
卡勒布·克利夫特

@CalebKleveter谢谢!很高兴您发现它令人印象深刻:)
Xcoder先生17年


3

JavaScript(ES6),52个字节

f=(m,k=m,x={})=>k?f(x[k*k%m]=m,k-1,x):Object.keys(x)

测试用例


非递归版本 60 58字节

由于@ThePirateBay,节省了2个字节

m=>(a=[...Array(m).keys()]).filter(v=>a.some(n=>n*n%m==v))

测试用例


非递归58个字节:m=>(a=[...Array(m).keys()]).filter(v=>a.some(n=>n*n%m==v))

@ThePirateBay好抓住。谢谢。
Arnauld


3

Brachylog10 9字节

>ℕ^₂;?%≜ᶠ

在线尝试!

说明

       ≜ᶠ       Find all numbers satisfying those constraints:
    ;?%           It must be the result of X mod Input where X…
  ^₂              …is a square…
>ℕ                …of an integer in [0, …, Input - 1]

我本来建议{>≜^₂;?%}ᵘ作为替代...然后我意识到也有负数。> _ <
暴民埃里克(Erik the Outgolfer)'17年

1
@EriktheOutgolfer一旦提交提交到TIO,我实际上可以使用将该答案减少到9个字节
致命

好...如果也有负数怎么办?它会简单地忽略它们还是什么?
暴民埃里克(Erik the Outgolfer)'17年

可以将@EriktheOutgolfer mod定义为除法的余数,这将是正数(商采用正负号)。编辑:而且,正方形是正的。
jaxad0127

@ jaxad0127我认为情况并非如此,因为>仍然会导致afaik为负数。
暴民埃里克(Erik the Outgolfer)'17年

3

Japt7 6字节

Dz%UÃâ

测试一下

由于使用了Oliver,节省了1个字节


说明

整数的隐式输入U

Ç   Ã

创建一个从0to到U-1(含)的整数数组,并将每个整数传递给一个函数。

²

广场。

%U

模数U

â

获取数组中的所有唯一元素并隐式输出结果。


1
我认为范围不必包含所有范围。Dz%UÃâ似乎工作正常。
奥利弗·


2

实际上,11个字节

;╗r⌠²╜@%⌡M╔

在线尝试!

说明:

;╗r⌠²╜@%⌡M╔
;╗           store a copy of m in register 0
  r          range(m)
   ⌠²╜@%⌡M   for n in range:
    ²          n**2
     ╜@%       mod m
          ╔  remove duplicates

2

CJam,12个字节

{_,_.*\f%_&}

匿名块接受一个数字并返回一个列表。

在线尝试!

说明

_,          Copy n and get the range [0 .. n-1]
  _.*       Multiply each element by itself (square each)
     \f%    Mod each by n
        _&  Deduplicate

真好!我只有{:X{_*X%}%_&}13个字节
Luis Mendo

2

Haskell,45个字节

import Data.List
f m=nub[n^2`mod`m|n<-[0..m]]

来自Anders Kaseorg的-4字节

在线尝试!


Sadly point-free version f m=nub$map((`mod`m).(^2))[0..m] is just as long, unless there is a sneaky syntax to get rid of extra parentheses.
shooqie




1

JavaScript (ES6), 48 bytes

f=
n=>[...new Set([...Array(n)].map((_,i)=>i*i%n))]
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

43 bytes if returning a Set instead of an array is acceptable.


1

Scala, 32 30 bytes

Simple use of the easy tip from OP.

(0 to n-1).map(x=>x*x%n).toSet

Try it online!

-2 bytes thanks to @MrXcoder, with priorities (no need for () around * operation)

Wondering: is this possible to implicitly tell the compiler to understand things like (0 to n-1)map(x=>x*x%n)toSet (without having to import scala.language.postfixOps)?


1
(0 to n-1).map(x=>x*x%n).toSet for 30 bytes. Exponentiation has higher precedence than modulo.
Mr. Xcoder

@Mr.Xcoder ooh~ thanks :)
V. Courtois


0

Retina, 70 bytes

.+
$*

;$`¶$`
1(?=.*;(.*))|;1*
$1
(1+)(?=((.*¶)+\1)?$)

D`1*¶
^|1+
$.&

Try it online! Warning: Slow for large inputs. Slightly faster 72-byte version:

.+
$*

$'¶$';
1(?=.*;(.*))|;1*
$1
+s`^((1+)¶.*)\2
$1
^1+

D`1*¶
^|1+
$.&

Try it online!



0

Perl 6, 19 bytes

{set (^$_)»²X%$_}

Test it

Expanded:

{ # bare block lambda with implicit param 「$_」

  set        # turn the following into a Set (shorter than 「unique」)

      (
        ^$_  # a Range upto (and excluding) 「$_」
      )»²    # square each of them (possibly in parallel)

    X%       # cross modulus the squared values by

      $_     # the input
}

0

Pyth, 13 bytes

VQ aY.^N2Q){Y

Try online.

Lame attempt at explaining:

VQ               for N in [0 .. input-1]
                   blank space to supress implicit print inside the loop
     .^N2Q         N ^ 2 % input
   aY              append that to Y, which is initially an empty list
          )      end for
           {Y    deduplicate and implicit print

To sort the output, insert an S on any side of the {

I think there should be a shorter way...


1
Yeah, the functional style of Pyth tends to be much more concise. map is your friend!
Anders Kaseorg





0

PHP, 53 bytes

for(;$i<$t=$argv[1];)$a[$z=$i++**2%$t]++?:print"$z
";

Loop from 0 to the input number, using the n^2 mod base formula to mark numbers that have been used. It goes to that position in an array, checking if it's been incremented and outputting it if it hasn't. It then increments it afterwards so duplicate values don't get printed.

Try it online!


0

8th, 138 131 bytes

Code

[] swap dup >r ( 2 ^ r@ n:mod a:push ) 1 rot loop rdrop ' n:cmp a:sort ' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip

Explanation

[] - Create output array

swap dup >r - Save input for later use

( 2 ^ r@ n:mod a:push ) 1 rot loop - Compute square end

rdrop - Clean r-stack

' n:cmp a:sort - Sort output array

' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip - Get rid of consecutive duplicates from array

SED (Stack Effect Diagram) is: a -- a

Usage and example

: f [] swap dup >r ( 2 n:^ r@ n:mod a:push ) 1 rot loop rdrop ' n:cmp a:sort ' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip ;

ok> 120 f .
[0,1,4,9,16,24,25,36,40,49,60,64,76,81,84,96,100,105]

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.