多少个素数?


14

表示自然数的一种方法是乘以质数的指数。例如,6可以由2 ^ 1 * 3 ^ 1表示,而50可以由2 ^ 1 * 5 ^ 2表示(其中^表示指数)。与其他方法相比,此表示形式中的质数可以帮助确定使用这种表示方法是否更短。但是因为我不想手工计算这些,所以我需要一个程序为我做这些。但是,由于在回家之前我必须记住该程序,因此它必须尽可能短。

你的任务:

编写程序或函数以确定该数字表示形式中有多少个不同的质数。

输入:

通过任何常规方法获取的整数n,使得1 <n <10 ^ 12。

输出:

引言中概述了表示输入所需的不同素数的数量。

测试用例:

24      -> 2 (2^3*3^1)
126     -> 3 (2^1*3^2*7^1)
1538493 -> 4 (3^1*11^1*23^1*2027^1)
123456  -> 3 (2^6*3^1*643^1)

这是OEIS A001221

得分:

这是,最低得分(以字节为单位)获胜!


3
最近有很多主要问题!我喜欢它。
朱塞佩


3
反对投票的原因可能是它的琐碎性。据我所见,关于高尔夫语言,有3种情况:1.内置2.两个内置链3.内置3个链(我个人有3个2字节的答案);我不知道这是否是
拒绝投票的

1
可以,但是如果三位投票者之一对此发表评论,我将不胜感激。尽管在高尔夫语言中这微不足道的,但在非高尔夫语言中有一些有趣的解决方案,这是我在发布此挑战时希望看到的解决方案。毕竟,网站上存在许多挑战,这些挑战对于golflangs而言是微不足道的,但会产生有趣的非golanglang解决方案。

1
在测试用例中包括一个素数将是有益的。同样,某些语言/方法很难测试大量。一些较小的测试用例会很好。
丹尼斯,

Answers:


6

MATL4 3字节

-1字节感谢Luis Mendo

YFz

在线尝试!

YF         Exponents of prime factors
  z        Number of nonzeros

原始答案:

Yfun

在线尝试!

一个正确的Yfun答案。

          (Implicit input)
Yf         Prime factorization
  u        Unique
   n       Numel
           (Implicit output)

1
为什么好玩?- ;-)
亚当

1
划掉4仍然是常规4
Gryphon

5

05AB1E,2个字节

另一个很无聊的答案...

fg

接受数字输入并打印结果的完整程序

在线尝试!

怎么样?

fg - implicitly take input
f  - get the prime factors with no duplicates
 g - get the length
   - implicit print

5

Mathematica,7个字节

PrimeNu

是的,有一个内置的。

Mathematica,21个字节

Length@*FactorInteger

很长的路要走。


星号的原因是什么?不Length@FactorInteger一样吗
numbermaniac

1
Length@*FactorInteger产生一个纯函数:的组成LengthFactorInteger。我可以定义fun=Length@*FactorInteger然后致电fun[1001]。另一方面,Length@FactorInteger将表示Length[FactorInteger]并求值为0
Misha Lavrov


4

Python 2,56个字节

f=lambda n,p=2,k=1:n/p and[f(n,p+1),k+f(n/p,p,0)][n%p<1]

这是丹尼斯的回答的端口这里或许?
乔纳森·艾伦,

1
@JonathanAllan是的,已修改为计算唯一的主要因子。
Orlp

4

视网膜31 30字节

&`(?!(11+)\1+$)(11+)$(?<=^\2+)

输入为一元。

感谢@MartinEnder进行1字节的高尔夫!

在线尝试!(包括十进制到一进制转换器)

怎么运行的

由于该程序由单个正则表达式和 &修饰符,因此Retina只需计算重叠匹配的数量即可。假定输入由n个重复1组成,没有其他内容。

负面的前瞻

(?!(11+)\1+$)

在之间的位置匹配1的那些遵循由两个或更多个1的(11+),随后在相同量的一个或多个重复1的(\1+),随后输入结束($)。

任何合数ABA,B> 1可以写为b的重复一个的重复1,所以先行仅位置,随后匹配p的重复 1,其中P = 1p是素数。

正则表达式

(11+)$

确保P> 1通过要求至少两个1的(11+),并且存储的尾1 'S中的第二捕获组(\2)。

最后,积极的回望

(?<=^\2+)

验证整个输入是否包含kp次出现(k≥1)为1,并验证p对输入进行了除法。

因此,每个匹配项对应一个唯一的素数除数p


4

Bash + GNU实用程序,33

  • @Dennis节省了1个字节
factor|grep -Po ' \d+'|uniq|wc -l

在线尝试

说明

factor|                            # Split input into prime factors
       grep -Po ' \d+'|            # group factors onto lines
                       uniq|       # remove duplicates
                            wc -l  # count the lines

1
grep -Po ' \d+'保存一个字节tr \ \\n|sed 1d
丹尼斯,

不幸的是,grep -Po '( \d+)\1*'输入46失败。
丹尼斯

@Dennis谢谢-我是根据您的原始建议修正的
Digital

3

果冻,3 个字节

一个很无聊的答案...

ÆFL

单数链接,获取一个数字并返回一个数字

在线尝试!

怎么样?

ÆFL - Link: number, n
ÆF  - prime factorisation as a list of prime, exponent pairs
  L - length

1
你是怎么想念的Æv
我的代名词是monicareinstate '17

这很容易-我从未使用过它,也没有在Wiki上搜索列表。
乔纳森·艾伦

您如何键入没有原子列表和快速列表的果冻字符?
我的代词是monicareinstate '17

1. Æ是替代代码0198。2.您可以设置键盘(我没有)。3.代码页。
乔纳森·艾伦



3

爱丽丝,10字节

/o
\i@/Dcd

在线尝试!

说明

/o
\i@/...

这只是需要十进制I / O的线性算术繁重程序的标准框架。实际的程序本身就是:

Dcd

哪个:

D    Deduplicate prime factors. Does what it sounds like: for every p^k which
     is a divisor n, this divides n by p^(k-1).
c    Push the individual prime factors of n. Since we've deduplicated them
     first, the number of factors is equal to the value we're looking for.
d    Push the stack depth, i.e. the number of unique prime factors.

3

JavaScript 45字节

*对于@SEJPM,请提供一个解释:我在这里所做的是-我从2-n开始变化(这将改变,最终将是最大的素数)-现在,如果当前数字除以ni,则只希望对其计数一次(甚至尽管它可能是2 * 2 * 2 * 3的因数-2被计数一次)-所以“ j”出现在图片中,当在函数调用中未指定j时-j将收到“ undefined”,然后当n%i == 0时,我在下一次调用中调用j = 1的函数)-然后,当j等于undefined时,我才加1,即!j + Function(n / i,i,( j = 1或仅1))。我不会在这件事上更改i,因为它仍然可以被i再次整除(2 * 2 * 3),但是j将等于1,并且不会被视为一个因子。希望我解释得足够好。

P=(n,i=2,j)=>i>n?0:n%i?P(n,i+1):!j+P(n/i,i,1)

console.log(P(1538493)==4);
console.log(P(24)==2);
console.log(P(126)==3);
console.log(P(123456)==3);

如果最后一个素数非常大,它将超过最大调用堆栈数-如果它是一个问题,我可以进行迭代


您介意为此答案写一个解释吗?其余答案似乎都使​​用了通常的方法。
SEJPM

@SEJPM我在此处添加了一些解释
DanielIndie '17

1
仅供参考,对于大多数代码高尔夫球挑战,我们可以假定无限调用堆栈/无限资源(基本上,除非问题另有说明)。
乔纳森·艾伦,








2

R + numbers, 30 14 bytes

16 bytes removed thanks to @Giuseppe

numbers::omega

Also, here is the Try it online!! link per @Giuseppe.


You may omit the f=function(x) and the (x) as numbers::omega is a function already. However, as numbers is not standard for R, you should make your answer "R + numbers". Also, you should include a TIO link. Still, +1, very nice.
Giuseppe

@Giuseppe, you are too nice. Thanks for your help. BTW, in addition to some of your insightful answers, I checked out Tips for golfing in R, as you suggested. There are some real gems there. Anywho, I will update my answer with your recommendations. Also, your MATL solution is very nice (+1 yesterday).
Joseph Wood

NP, feel free to ping me in chat or comment on an answer of mine if you have questions.
Giuseppe

@Giuseppe is there a meta consensus on needing to explicitly state "R + numbers"? It seems like if we state the additional package then we should be able to save the bytes of explicitly calling it with numbers::. Otherwise, to me it's the same as using an import in any other language.
BLT

(scrolls down and sees a python example of this...) I guess I'm wondering about a broader meta consensus, then. It just sort of seems silly to me.
BLT



1

Haskell, 58 bytes

-4 bytes thanks to @Laikoni

f n=sum[1|x<-[2..n],gcd x n>1,all((>)2.gcd x)[2..x-1]]

Try it online!

Explanation

Essentially generates all primes at most as large as n and filters them for being a factor of n and then takes the length of the result.

f n=                                                   -- main function
    sum[                                             ] -- output the length of the list
        1|x<-[2..n],                                   -- consider all potential primes <=n
                                                       -- and insert 1 into the list if predicates are satisfied
                    gcd x n>1,                         -- which are a factor of n
                              all(          )[2..x-1]  -- and for which all smaller numbers satisfy
                                  (>)2.                -- 2 being larger than
                                       gcd x           -- the gcd of x with the current smaller number

You can use sum[1|x<- ... ] instead of length.
Laikoni

1

Japt, 5 4 bytes

â èj

Try it

Get the divisors (â) and count (è) the primes (j).


1

ARBLE, 28 bytes

len(unique(primefactors(n)))

Try it online!

This is a very literal solution


I was looking at this and going "Hey, wait a minute, this is a snippet!" And then I see... is this supposed to be a non-esoteric language with implicit IO?!
totallyhuman

@icrieverytim Congratulations, you have discovered one of the main reasons this language exists.
ATaco


0

Python 2,  63  55 bytes

A much more interesting answer...

-8 bytes thanks to Jonathan Frech (use an argument with a default for the post-adjustment of the result of primes from 0 to 1 -- much better than a wrapping lambda!!)

f=lambda n,o=1:sum(n%i+f(i,0)<1for i in range(2,n))or o

A recursive function taking a positive integer, n, and returning a positive integer, the count.

Try it online! Really inefficient, don't even bother with the other test cases.



@JonathanFrech Thanks, that is much cleaner.
Jonathan Allan

0

J, 12 bytes

{:@$@(__&q:)

q: is J's prime exponents function, giving it the argument __ produces a matrix whose first row is all nonzero prime factors and whose 2nd row is their exponents.

We take the shape $ of that matrix -- rows by columns -- the number of columns is the answer we seek.

{: gives us the last item of this two items (num rows, num columns) list, and hence the answer.

Try it online!



0

Javascript ES6, 56 chars

n=>eval(`for(q=2,r=0;q<=n;++q)n%q||(n/=q,r+=!!(n%q--))`)

Test:

f=n=>eval(`for(q=2,r=0;q<=n;++q)n%q||(n/=q,r+=!!(n%q--))`)
console.log([24,126,1538493,123456].map(f)=="2,3,4,3")

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.