计算一个数的除数


26

介绍

这是一个非常简单的挑战:只需计算一个数的除数即可。以前我们遇到过类似但更复杂的挑战,但我打算将此作为入门级。

挑战

创建一个程序或函数,给定一个严格的正整数N,该程序或函数输出或返回其具有的除数,包括1和N

输入:一个大于0的整数。您可以假设该数字可以用您的语言的本机数字类型表示。

输出:它具有的正整数除数的数量,包括1和数字本身。

提交内容将以字节计分。尽管您可以使用任何合理的方法来生成字节数,但您可能会发现此网站很方便。

这是,因此最低分获胜!

编辑:看来FryAmTheEggman的5个字节的Pyth答案是赢家!不过,请随时提交新答案。如果您能得到更短的答案,我将更改接受的答案。

测试用例

ndiv(1) -> 1
ndiv(2) -> 2
ndiv(12) -> 6
ndiv(30) -> 8
ndiv(60) -> 12
ndiv(97) -> 2
ndiv(100) -> 9

排行榜

这是一个堆栈片段,用于按语言生成常规排行榜和获胜者概述。

为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

# Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以通过打败旧分数保持标题。例如:

# Ruby, <s>104</s> <s>101</s> 96 bytes

如果要在标题中包含多个数字(例如,因为您的分数是两个文件的总和,或者您想单独列出解释器标志的惩罚),请确保实际分数是标题中的最后一个数字:

# Perl, 43 + 2 (-p flag) = 45 bytes

您还可以将语言名称设置为链接,然后该链接将显示在页首横幅代码段中:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

Answers:


19

Pyth,5岁

l{yPQ

对输入的主要因子使用子集运算,然后仅保留因子的唯一列表并返回此计数。

测试套件

说明

以25为例,子集列表不会很长

l{yPQ     ## implicit:  Q = eval(input()) so Q == 25
   PQ     ## Prime factors of Q, giving [5, 5]
  y       ## All subsets, giving [[], [5], [5], [5, 5]]
 {        ## Unique-fiy, giving [[], [5], [5, 5]]
l         ## Length, print implicity

迷人。好的方法
Cyoce '16


11

LabVIEW,4938字节

好吧,它显然不适合打代码高尔夫,但是无论如何,所以对于我的第一篇文章和这里的lolz来说都是如此。 在此处输入图片说明


欢迎来到编程难题和Code Golf!如果您不介意我问,您如何评分?我在Meta上找不到任何先例。
bkul 2015年

我保存并取了它的大小
Eumel,2015年

它是4.938字节?例如,不是千字节?
bkul

为了准确
起见,

4
@bkul我认为混乱是由于引起的.,我敢肯定,它是千位分隔符,而不是小数点(在某些语言中很常见)。
马丁·恩德

10

Haskell,28个字节

f n=sum[0^mod n i|i<-[1..n]]

这里的窍门是测试剩余部分是否正在0使用指标功能0^

0^0 = 1
0^_ = 0

之所以可行,是因为0的任何正幂为0,而0 ^ 0组合为1的空乘积。

比较一下过滤

f n=sum[1|i<-[1..n],mod n i<1]

7

Dyalog APL7 6 字节

≢∘∪⊢∨⍳

它是一个未命名的函数,可以对每个(¨)测试用例进行命名,然后对其进行重用,如下所示:

      f ← ≢∘∪⊢∨⍳
      f¨ 1 2 12 30 60 97 100
1 2 6 8 12 2 9

说明:

 ┌─┴──┐  
 ∪  ┌─┼─┐
 ∘  │ ∨ │
 ̸≡  ⊢   ⍳

计算其自身和每个整数(直到)的GCD 的唯一性。

感谢ngn保存一个字节。


旧版: +/0=⍳|⊢

它是这样工作的:

  ┌─┴─┐      
  / ┌─┼───┐  
┌─┘ 0 = ┌─┼─┐
+       ⍳ | ⊢

⍳|⊢1-through-argument除法余数自变量
0=Boolean如果0等于布尔除法除法之
+/和,即1的计数。


6

Python 2,37个字节

f=lambda n,i=1:i/n or(n%i<1)+f(n,i+1)

递归函数。i被测试的除数中的可选输入。该表达式(n%i<1)测试除数,除数True等于(等于1)。将结果添加到的追溯表达式中i+1。当i==n到达时,整数地板除法i/n计算结果为1,并且该值被返回作为碱的情况下,占n本身为的除数n


38:

lambda n:sum(n%-~i<1for i in range(n))

匿名函数。1通过测试所有可能的除数n。这是从上移0,通过n-1range(n)使用-~,它增加了1。对布尔值求和使用了Python将True/ False视为1/ 的事实0


6

视网膜,17字节

(?<=(.+))(?=\1*$)

一元输入,十进制输出。

在线尝试。

当使用单个正则表达式调用时,Retina会简单地计算匹配项。正则表达式本身与一个位置匹配,该位置左侧的一元数是整个输入的除数。我还利用了环视是原子的这一事实,因此不需要使用^锚。

后面的第一个条件只是捕获group中的整个前缀1。这将永远不会失败,因此在进行了回望之后,我们知道这就是组1中的内容,并且它将不再更改。

然后,超前检查通过将捕获的字符串(我们的潜在除数)重复0次或更多次来检查是否可以到达字符串的末尾。


6

J,10个字节

[:*/1+_&q:

这是一个未命名的单子词动词。它计算σ 0(率Πp ķ α ķ作为Π(α ķ + 1)

使用J.js在线尝试。

怎么运行的

[:*/1+_&q:    Right argument: y

      _&q:    Compute all exponents of the prime factorization of y.
    1+        Add 1 to each exponent.
[:*/          Reduce by mutiplication.

我认为q:不允许这样做,因为它解决了挑战的关键部分。怎么样[:+/0=]|~1+i.
FUZxxl

那将是这个答案的重复。另外,默认情况下也不禁止内置,挑战似乎也没有提到它们。
丹尼斯

通常禁止从事挑战的所有/几乎所有工作的内置工具,但是我可以遵循你的推理q:
FUZxxl

1
他们不是。我希望他们是,但不是。
丹尼斯

有点烂的Hrmpf hrmpf。
FUZxxl

6

Golfscript,19 18 17 13个字节

感谢MartinBüttner

~.,\{\)%!}+,,

怎么运行的

~               Evaluate the input, n
 .,             Duplicate the input, create array [0..n-1]
   \            Swap array and n
    {    }+     Add n to block == {n block}
     \          Swap n with i in array
      )         Increment i
       %        n mod i
        !       Logical not so that 1 if divisible by n else 0
           ,    Filter array using block for all i divisible by n
            ,   Get length of the filtered array, the answer

来自@Peter Taylor,也是13个字节。

~:X,{)X\%!},,

怎么运行的

~               Evaluate the input
 :X             Store input in variable X
   ,            Create array [0..X-1]
    {     },    Filter array using the following block
     )          Increment i in array
      X\        Add X to stack, swap with i
        %       X mod i,
         !      Logical not so that 1 if divisible by n else 0
            ,   Get length of the filtered array, the answer

对于相同的长度,您也可以拥有~:X,{)X\%!},,
Peter Taylor

4

J,13 12 11字节

我在J的第一场高尔夫。我还在学习。

感谢Dennis,节省了一个字节。

多亏randomra节省了一个字节。

1+/@,0=i.|]

说明:

1+/@,0=i.|]
       i.        the array 0 .. n-1
         |]      mod n
     0=          replace 0 by 1, and nonzero entries by 0
1   ,            prepend 1 to the array
 +/@             take the sum

3

Arcyóu,12个字节

让我们开始派对吧!

(F(x)(_(d/ x

这使用了内置功能d/。这是一个没有内置的版本(27字节):

(F(x)(](+(f i(_ 1 x)(‰ x i

说明:

(F(x)              ; Anonymous function with one parameter x
  (]               ; Increment
    (+             ; Sum
      (f i(_ 1 x)  ; For i in range from 1 to x-1 inclusive:
        (‰ x i     ; x divisible by i

3

CJam,11个字节

ri_,:)f%0e=

在这里测试。

说明

CJam对此没有内置的功能,因此我们正在做试算。

ri  e# Read input and convert to integer N.
_,  e# Duplicate and turn into range [0 1 ... N-1]
:)  e# Increment each element in the range to get [1 2 ... N]
f%  e# Take N modulo each of the list elements.
0e= e# Count the zeroes.

奖金

这是一个有趣的12字节的解决方案(我怀疑这在像J这样的语言中可能是最短的):

ri_)2m*::*e=

结果等于乘法表中n出现的次数n x n

ri  e# Read input and convert to integer N.
_)  e# Duplicate and increment.
2m* e# Take Cartesian product of [0 1 ... N] with itself.
::* e# Compute the product of each pair.
e=  e# Count the occurrences of N.

3

Matlab,20个字节

k mod n对each 执行一次k = 1,...,n,然后执行not(将每个nonzer变为零,将每个零变为1),并对所有这些值求和。

@(n)sum(~mod(n,1:n))

这也将是我的方法!
路易斯·门多

有趣的是,这与的长度相同length(divisors(n))
累积

@累积,您仍然需要添加一个@(n)使其成为有效的submisison
瑕疵

3

朱莉娅,20个字节

n->sum(i->n%i<1,1:n)

这是一个匿名函数,其工作方式如下:对于从1到输入的每个整数,测试输入是否对整数取模为零。如果是这样,则该值为true,否则为false。我们对布尔值求和,这些布尔值隐式转换为整数,得出除数的数量。


为了完整起见,包括了一个更凉爽(虽然也更长)的解决方案

n->prod(collect(values(factor(n))).+1)

这将得到的规范因式分解n,即\prod_{i=1}^k p_i^e_i,并将除数函数计算为τ(n) = \prod_{i=1}^k e_i + 1




2

Ruby,27个字节

->n{(1..n).count{|i|n%i<1}}

样品运行:

2.1.5 :001 > ->n{(1..n).count{|i|n%i<1}}[100]
 => 9 


2

正则表达式(.NET),33字节

^((?=.*$(?<=^\2*(.+?(?>\2?)))).)+

假设输入和输出为一元,则输出取自正则表达式的主匹配项。

分解正则表达式:

  • .*$ 将指针设置为字符串的末尾,以使整个输入x处于一个方向。
  • (?<=^\2*(.+?(?>\2?))) 从右到左匹配,并从x到0循环检查除数。
    • (.+?(?>\2?)) 是一个“变量”,它在第一次迭代中从1开始,并从先前迭代中的数字开始,一直循环到x。
    • ^\2* 检查x是否为“变量”的倍数。

它基本上与我对Calculate Phi(不是Pi)的回答具有相同的想法。只有支票不同。

RegexStorm中测试正则表达式。


2

迷宫,33字节

?:}
  :{:}%{{
@ }   " )
!{("{;"}}

在线尝试。

这实现了审判部门。稍后我将添加完整的解释。这可能不是最佳选择,但我很难提出更短的内容。



2

Javascript(ES6),60 57 42 40 39 37字节

这可能打得更好。

n=>{for(d=i=n;i;n%i--&&d--);return d}

编辑1:我是对的。在for循环后删除括号。

编辑2: Golfed到40个字节,感谢manatwork马丁布特内尔

编辑3:通过将函数基于上述C答案来保存字节

编辑4:感谢ןnɟuɐɯɹɐןoɯNeil,但我无法评估。

编辑5:忘记删除评估。

测试

n = <input type="number" oninput='result.innerHTML=(

n=>{for(d=i=n;i;n%i--&&d--);return d}

)(+this.value)' /><pre id="result"></pre>


2
放弃好习惯。删除var关键字。JavaScript中的打高尔夫球技巧ECMAScript 6中的打高尔夫球技巧中的更多技巧。
manatwork

2
还要放弃坏习惯:当您在++i和之间做出选择时i++,选择前者(这与打高尔夫球无关)。还n%i<1应该保存一个字节。
马丁·恩德

2
仅经过简短测试:n=>{for(d=i=0;i<n;)n%++i<1&&d++;return d}
manatwork

1
38:n => eval('for(d = 0,i = n; i; d + = n%i-<1); d')
Mama Fun Roll

1
@manatwork为什么不n%++i||++d呢?
尼尔

2

PowerShell,34个字节

param($x)(1..$x|?{!($x%$_)}).Count

e.g. 

PS C:\temp> .\divisors-of-x.ps1 97
2
  • 创建一个从1到x的数字列表,并将其输入管道 |
  • 过滤(x%item == 0)上的管道,方法是将模结果隐式转换为布尔值,然后使用求反,!从而使除数变为$ true并被允许通过;使用内置别名?Where-Object
  • 收集()以及有.Count多少物品通过过滤器

非常好被黑!
bkul 2015年

2

果冻,2个字节(再次不竞争)

Æd

在线尝试!

认为这使用了在其他果冻答案之后实现的功能。如果我错了,请发表评论(您知道行中的每个提交都看不到:))


2

出租车,2143字节

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Cyclone.Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 1 r.Go to Cyclone:n 1 l.Pickup a passenger going to Firemouth Grill.Pickup a passenger going to Joyless Park.Go to Firemouth Grill:s 1 l 2 l 1 r.Go to Joyless Park:e 1 l 3 r.[i][Check next value n-i]Go to Zoom Zoom:w 1 r 2 l 2 r.Go to Sunny Skies Park:w 2 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to Sunny Skies Park.Go to Joyless Park:n 2 r 2 r 2 l.Pickup a passenger going to Cyclone.Go to Sunny Skies Park:w 1 r 2 l 2 l 1 l.Go to Cyclone:n 1 l.Pickup a passenger going to Joyless Park.Pickup a passenger going to Divide and Conquer.Go to Divide and Conquer:n 2 r 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Equal's Corner.Go to Trunkers:s 1 l.Pickup a passenger going to Equal's Corner.Go to Equal's Corner:w 1 l.Switch to plan "F" if no one is waiting.Pickup a passenger going to Knots Landing.Go to Firemouth Grill:n 3 r 1 l 1 r.Pickup a passenger going to The Underground.Go to The Underground:e 1 l.Pickup a passenger going to Firemouth Grill.Go to Knots Landing:n 2 r.Go to Firemouth Grill:w 1 l 2 r.Go to Joyless Park:e 1 l 3 r.Switch to plan "N".[F][Value not a divisor]Go to Joyless Park:n 3 r 1 r 2 l 4 r.[N]Pickup a passenger going to The Underground.Go to The Underground:w 1 l.Switch to plan "E" if no one is waiting.Pickup a passenger going to Joyless Park.Go to Joyless Park:n 1 r.Switch to plan "i".[E]Go to Sunny Skies Park:n 3 l 2 l 1 l.Pickup a passenger going to What's The Difference.Go to Firemouth Grill:s 1 l 1 l 1 r.Pickup a passenger going to What's The Difference.Go to What's The Difference:w 1 l 1 r 2 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:e 3 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.

在线尝试!

取消高尔夫:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left 2nd right.
Pickup a passenger going to Cyclone.
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park: north 1st right.
Go to Cyclone: north 1st left.
Pickup a passenger going to Firemouth Grill.
Pickup a passenger going to Joyless Park.
Go to Firemouth Grill: south 1st left 2nd left 1st right.
Go to Joyless Park: east 1st left 3rd right.
[i]
[Check next value n-i]
Go to Zoom Zoom: west 1st right 2nd left 2nd right.
Go to Sunny Skies Park: west 2nd left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to Sunny Skies Park.
Go to Joyless Park: north 2nd right 2nd right 2nd left.
Pickup a passenger going to Cyclone.
Go to Sunny Skies Park: west 1st right 2nd left 2nd left 1st left.
Go to Cyclone: north 1st left.
Pickup a passenger going to Joyless Park.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer: north 2nd right 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st left 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Equal's Corner.
Go to Trunkers: south 1st left.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner: west 1st left.
Switch to plan "F" if no one is waiting.
Pickup a passenger going to Knots Landing.
Go to Firemouth Grill: north 3rd right 1st left 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: east 1st left.
Pickup a passenger going to Firemouth Grill.
Go to Knots Landing: north 2nd right.
Go to Firemouth Grill: west 1st left 2nd right.
Go to Joyless Park: east 1st left 3rd right.
Switch to plan "N".
[F]
[Value not a divisor]
Go to Joyless Park: north 3rd right 1st right 2nd left 4th right.
[N]
Pickup a passenger going to The Underground.
Go to The Underground: west 1st left.
Switch to plan "E" if no one is waiting.
Pickup a passenger going to Joyless Park.
Go to Joyless Park: north 1st right.
Switch to plan "i".
[E]
Go to Sunny Skies Park: north 3rd left 2nd left 1st left.
Pickup a passenger going to What's The Difference.
Go to Firemouth Grill: south 1st left 1st left 1st right.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: west 1st left 1st right 2nd right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: east 3rd right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.

说明:

Convert stdin to a number and store it in three locations for three purposes:
   Original (Sunny Skies Park)
   Counter for tested values (Joyless Park)
   Counter for divisors found (Firemouth Grill)
Divide the original by each Joyless Park value in turn.
If the division result equals the truncated division result, then it's a divisor.
When a divisor is found, subtract one from Firemouth Grill.
Repeat until Joyless Park hits zero.
Pickup the original from Sunny Skies Park and subtract the value from Firemouth Grill.
Convert the result to a string and print to stdout.


2

Excel公式,42 28字节

编辑:我只是意识到我不需要使用INDIRECT,节省14个字节!

以下内容应作为数组公式(Ctrl+ Shift+ Enter)输入:

=SUM(--NOT(MOD(N,ROW(1:N))))

其中N是要测试的数字。

例子:

{SUM(--NOT(MOD(32,ROW(1:32))))}
Result: 6
{SUM(--NOT(MOD(144,ROW(1:144))))}
Result: 15

说明:

SUM(--NOT(MOD(N,ROW(1:N))))       Full formula

                ROW(1:N)          Generates an array of row numbers e.g {1;2;3;4;...N}
          MOD(N,ROW(1:N))         Does N MOD {1;2;3;4;,...N}
      NOT(MOD(N,ROW(1:N)))        Coerces zeros to ones, so that they may be counted, but actually returns an array of TRUE;FALSE;FALSE;...
    --NOT(MOD(N,ROW(1:N)))        Coerces the TRUEs to 1s and FALSEs to 0s.
SUM(--NOT(MOD(N,ROW(1:N))))       Sum the ones for the result.



1

Minkolang 0.13,16个字节

ndd[0ci1+%,-]-N.

在这里检查所有情况。

说明

ndd           Takes number from input and duplicates it twice (n)
[             Opens for loop that runs n times
 0c           Copies bottom of stack to top (n)
   i1+        Loop counter + 1 (d)
      %       Modulo - pops d,n, then pushes n%d
       ,      Not - 1 if equal to 0, 0 otherwise
        -     Subtract
         ]    Close for loop
-             Subtract (n - 1 for each non-divisor)
N.            Output as number and stop.
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.