贫因数


20

如果的正整数具有(严格地)比其后继者和前任者少的素数(不计算多重性),我们将其称为因数贫乏数。ñ>2

换句话说,和,其中是独特的素因数的数。ω (Ñ )< ω (Ñ + 1 )ω (Ñ )ñω(N)<ω(N−1)ω(N)<ω(N+1)ω(N)N

任务

您可以在以下I / O格式中进行选择:

  • 取整数并输出因数贫乏的。如果选择此选项,则 可以是0或1索引。Ñ 第 ÑNNthN
  • 取一个正整数并输出前因数贫乏的数字。NNN
  • 无限期打印序列。

您可以使用任何编程语言,通过任何标准方法接受输入并提供输出,同时请注意,默认情况下禁止这些漏洞。这是代码高尔夫球,因此遵守规则的最短提交将获胜。

我将不包括单独的测试用例,因为竞争的方法不同,但是您可以参考此序列的前100个术语,即OEIS A101934:

11, 13, 19, 23, 25, 27, 29, 37, 41, 43, 47, 49, 53, 59, 61, 64, 67, 71, 73, 79, 81, 83, 89, 97, 101, 103, 107, 109, 113, 121, 125, 131, 137, 139, 149, 151, 155, 157, 163, 167, 169, 173, 179, 181, 191, 193, 197, 199, 211, 221, 223, 227, 229, 233, 239, 241, 243, 251, 259, 263, 265, 269, 271, 277, 281, 283, 289, 293, 307, 309, 311, 313, 317, 331, 337, 341, 343, 347, 349, 353, 359, 361, 365, 367, 371, 373, 379, 383, 389, 397, 401, 407, 409, 419, 421, 431, 433, 439, 441, 443

作为一个例子,发生在这个序列因为(5),(2和13)和(2和3),所以和。ω (25 )= 1 ω (26 )= 2 ω (24 )= 2 ω (25 )< ω (24 )ω (25 )< ω (26 )25ω(25)=1ω(26)=2ω(24)=2ω(25)<ω(24)ω(25)<ω(26)


我可以n = 在每个值之前输出前导吗?
— Steadybox

@Steadybox Sketchy,但我允许它:-/
— Xcoder先生18年

我将其添加为替代版本。
— Steadybox '18年

Answers:


7

Brachylog,21个字节

⟨+₁≡-₁⟩{ḋdl}ᵐ⌋>~↰₂?ẉ⊥

在线尝试!

无限打印。

说明

⟨+₁≡-₁⟩                  Fork: The output of the fork is [Input + 1, Input -1]
       {   }ᵐ            Map:
        ḋdl                (predicate 2) the output is the length of the prime decomposition
                           of the input with no duplicates
             ⌋           Take the minimum result of that map
              >          This minimum is bigger than…
               ~↰₂?      …the output of predicate 2 with Input as input
                  ?ẉ     Write Input followed by a new line if that's the case
                    ⊥    False: backtrack and try another value for Input

5

果冻,13 12字节

Cr~ÆvÐṂN⁼Wø#

打印前n个因子不足的数字。

在线尝试!

怎么运行的

Cr~ÆvÐṂN⁼Wø#  Main link. No arguments.

          ø   Wrap the links to the left into a chain and begin a new chain.
           #  Read an integer n from STDIN and call the chain to the left with
              arguments k = 0, 1, 2, ... until n of them return a truthy value.
              Return those n values of k as an array.
C                 Complement; yield -k+1.
  ~               Bitwise NOT; yield -k-1.
 r                Range; yield [-k+1, -k, -k-1].
     ÐṂ           Yield those elements of [-k+1, -k, -k-1] for which the link to
                  the left returns the minimal value.
   Æv                 Count the number of unique prime factors.
                      Note that, for a negative argument, Æv counts -1 as well, and
                      0 is counted as a/the factor of 0. Negating the the arguments
                      eliminates the edge case 1 (no factors), which would be a
                      false positive otherwise.
                  For a factor-poor number, this yields [-k].
       N          Take the negatives of the resulting integers.
         W        Wrap; yield [k].
        ⁼         Test the results to both sides for equality.

5

Python 2中,123个 119字节

q=lambda n:sum(n%i<all(i%j for j in range(2,i))for i in range(2,n+1))
i=2
while 1:
 i+=1
 if q(i-1)>q(i)<q(i+1):print i

在线尝试!


@FryAmTheEggman谢谢!即使我没有使用您的建议,它也启发了我节省了4个字节的另一种方法:D
— Rod

真好!我确定有办法避免两个丑陋的lambda :)
— FryAmTheEggman

4

MATL,26 24 22字节

`T@3:q+YFg!sdZSd0>?@QD

无限期打印序列。

在线尝试!

说明

`         % Do...while loop
  T       %   Push true. Will be used as loop condition
  @       %   Push (1-based) iteration index, k 
  3:q     %   Push [1 2 3] minus 1, that is, [0 1 2]
  +       %   Add, element-wise. Gives [k k+1 k+2]
  YF      %   Exponents of prime-factor decomposition. Gives a 3-row matrix
  g       %   Convert to logical: non-zero numbers become 1
  !s      %   Transpose, sum of each column. Gives a row vector of 3 elements, 
          %   which are the number of unique prime factors of k, k+1 and k+2 
  d       %   Consecutive differences. Gives a row vector of 2 elements
  ZS      %   Sign: replaces each number by -1, 0 or 1
  d       %   Consecutive difference. Gives a single number
  0>      %   Is it positive?
  ?       %   If so
    @Q    %     Push k+1
    D     %     Display
          %   End (implicit)
          % End (implicit). The stack contains true, which (is consumed and)
          % causes an infinite loop

3

外壳,22字节

f(ΠtSM<←ṙ1mȯLup§…←→)tN

无限期打印序列,在线尝试或查看第一个N!

或者§oΛ>←t 可以使用代替ΠtSM<←。

说明

f(                  )tN  -- filter the tail of the naturals ([2,3…]) by:
  ΠtSM<←ṙ1m(Lup)§…←→     -- | takes a number as argument, example 11
                §…       -- | range of..
                  ←      -- | | the argument decremented (10)
                   →     -- | | to the argument incremented (12)
                         -- | : [10,11,12]
          m(   )         -- | map the following (example on 12) ..
              p          -- | | prime factors: [2,2,3]
             u           -- | | deduplicate: [2,3]
            L            -- | | length: 2
                         -- | : [2,1,2]
        ṙ1               -- | rotate by 1: [1,2,2]
    SM<                  -- | map the function (< X) over the list where X is ..
       ←                 -- | | the first element (1)
                         -- | : [0,1,1]
   t                     -- | tail: [1,1]
  Π                      -- | product: 1
                         -- : [11,13,19,23,25,27,29…

3

Pyth,14个字节

.f!-.ml{Pb}tZh

在这里尝试!

最初是关于Dopapp答案的建议,但他们告诉我将其分开发布。

怎么运行的?

.f!-。ml {Pb} tZh | 完整程序。从STDIN接收输入,将第一个N输出到STDOUT。

.f | (var:Z)输出满足谓词的前N个值。
          } tZh | 创建列表[Z-1,Z,Z + 1]。
    .m | (var:b)选取功能值最小的元素。
        铅| b。的主要因素。
      l {| 重复数据删除,获取长度。
               | 对于因子贫乏的数字,其自身将包裹在一个单例中。
   -| 从中删除Z。
  !| 逻辑否定。

3

Haskell,105 86字节

感谢@Wheat向导,@ Bruce Forte和@Laikoni节省了19个字节。

[n|n<-[2..],d n<d(n-1),d n<d(n+1)] d x=[1|n<-[1..x],x`rem`n<1,all((>0).rem n)[2..n-1]]


当使用rem ==0和/=0可与relaced <1和>0分别。
— 小麦巫师

不需要将let定义d为辅助功能就可以了,请参见打高尔夫球规则指南。也sum可以省略,比较在列表上的作用相同。86个字节:在线尝试!
— Laikoni'1

2

八度, 87   83  79字节

感谢@Cows quack保存一个字节,感谢@Luis Mendo保存三个六个字节!

f=@(n)nnz(unique(factor(n)));n=9;while++n if[f(n-1) f(n+1)]>f(n)disp(n);end;end

无限期打印序列。

在线尝试!

73个字节,n =每个值前都有前导:

f=@(n)nnz(unique(factor(n)));n=9;while++n if[f(n-1) f(n+1)]>f(n)n end;end

在线尝试!


我认为该功能f可以f=@(n)length(unique(factor(n)))减少一个字节。
— Kritixi Lithos

2

05AB1E,14 13字节

输出第n个因子差的数字(1索引)

µ3LN+Íf€gÀć›P

在线尝试!

说明

µ                 # loop over N (1,2,3, ...) until counter equals input
 3L               # push the range [1,2,3]
   N+             # add current N
     Í            # subtract 2
      f           # get unique prime factors of each
       €g         # get length of each factor list
         À        # rotate left
          ć       # extract the head
           ›      # check if the remaining elements are strictly greater
            P     # product
                  # if 1, increment counter
                  # implicitly output final N

1
我只是建议不要切换到µ,所以我想指出一下我的替代方法- 如果N<N>Ÿ可以的3LN+Í话,可以替代。
— Xcoder先生18年

@ Mr.Xcoder:®XŸN+同样,也可以。或0®X)N+在这种情况下À将不需要。不幸的是,它们都以相同的字节数结束。
— Emigna

1

Pyth,30 25字节

#=hTI&>l{PhTKl{PT>l{PtTKT

这是我第一次真正的佩斯(Pyth)高尔夫,因此,任何意见都非常感谢。

非常感谢Xcoder!

说明

#                         | Loop until error
 =hT                      | Add one to T (initially 10)
    I&                    | If both...
      >l{PhTKl{PT         | The number of unique prime factors of T+1 is greater than that of T (store in K) 
                 >l{PtTK  | And the number of unique prime factors of T-1 is greater than K (from before)
                        T | Then implicitly print T

TIO。


14个字节:(.f!-.ml{Pb}tZh打印前n个)(.f检索满足条件的前n个值[1,2,3,...]并使用一个变量Z,}tZh生成整数范围[Z - 1 ... Z + 1],.m返回具有最小函数值的元素列表(带有b),l{Pb获取不同除数的数量,-丢弃Z从列表中,!应用逻辑非)
— Xcoder先生

1
@ Mr.Xcoder,哇,我想我不会很快得到那个!你不是说它应该有自己的答案吗?
— 丹尼尔(Daniel)

@Dopapp好吧,我单独发布了它。+1欢迎来到Pyth打高尔夫球!
— Xcoder先生18年

使用您的方法的25个字节。
— Xcoder先生18年

不。h是+1,t是-1,K而是是一个不带赋值的变量=。例如,K4将分配K给4。然后,您可以使用访问它K。
— Xcoder先生18年

1

JavaScript(ES6),94个字节

返回第N个因数贫乏的数字,索引为0。

f=(i,n=9)=>(P=(n,i=k=1)=>++k>n?0:n%k?P(n,1):i+P(n/k--,0))(n)>P(++n)&P(n)<P(n+1)&&!i--?n:f(i,n)

在线尝试!

怎么样?

我们首先定义P()函数,该函数返回给定整数的唯一质数的数量。

P = (                   // P = recursive function taking:
  n,                    //   n = input number to test
  i =                   //   i = flag to increment the number of prime factors
  k = 1                 //   k = current divisor
) =>                    //
  ++k > n ?             // increment k; if k is greater than n:
    0                   //   stop recursion
  :                     // else:
    n % k ?             //   if k is not a divisor of n:
      P(n, 1)           //     do a recursive call with n unchanged and i = 1
    :                   //   else:
      i +               //     add i and
      P(n / k--, 0)     //     do a recursive call with n / k and i = 0; decrement k

包装代码现在显示为:

f = (                   // given:
  i,                    //   i = input index
  n = 9                 //   n = counter
) =>                    //
  P(n) > P(++n) &       // increment n; if P(n - 1) is greater than P(n)
  P(n) < P(n + 1) &&    // and P(n) is less than P(n + 1)
  !i-- ?                // and we have reached the correct index:
    n                   //   return n
  :                     // else:
    f(i, n)             //   go on with the next value

1

Japt,29 27 26字节

对此并不完全满意,但至少比我第一次尝试超过40字节要好!

N以1索引的顺序输出第th个数字。

È=Jõ_+X k â ÊÃé)e>Xo)«´U}a

试试吧


说明

整数的隐式输入U。

È                       }a

返回X通过以下函数时返回true 的第一个整数。

=Jõ             )

将数组分配[-1,0,1]给X。

 _+X      Ã

通过该函数传递该数组的每个元素,该函数首先添加的当前值X。

k â Ê

获取结果的Ê唯一(â)个质因子(k)的长度()。

é

将结果数组向右旋转一个。

e>Xo)

弹出(o)中的最后一个元素,X并检查是否所有剩余元素都大于它。

«´U

如果是这样,请递减U并检查它是否等于0。


1

Python 3,97字节

n,g=9,lambda m,k=2,p=1:m//k and(m%k<p%k)+g(m,k+1,p*k*k)
while 1:n+=1;g(n-1)>g(n)<g(n+1)!=print(n)

从理论上讲,这将无限期地打印序列。实际上,g最终超过了递归限制。

在线尝试!



0

干净,130个 123 117字节

import StdEnv
?n=[1\\q<-[p\\p<-[2..n]|and[gcd p i<2\\i<-[2..p-1]]]|n rem q<1]
f=[n\\n<-[3..]| ?n<min(?(n-1))(?(n+1))]

等价于该序列的无数项。由于都是嵌套式的,因此无法很好地利用图归约的优势,因此即使对于如此糟糕的算法,它也相当慢。

在线尝试!


0

APL NARS,124字节,62个字符

{(⍵>1E4)∨⍵≤0:¯1⋄x←9+⍳10×⍵⋄⍵↑(∊{v←⍴∘∪∘π¨⍵+2-⍳3⋄2=+/v>2⌷v}¨x)/x}

它应该返回答案直到1E4,然后返回-1错误;假设9..10参数有足够的正确数字;测试:

  z←{(⍵>1E4)∨⍵≤0:¯1⋄x←9+⍳10×⍵⋄⍵↑(∊{v←⍴∘∪∘π¨⍵+2-⍳3⋄2=+/v>2⌷v}¨x)/x}  
  z 0
¯1
  z 1
11 
  z 20
11 13 19 23 25 27 29 37 41 43 47 49 53 59 61 64 67 71 73 79 

4
大约 150个字节?
— 粗野的

@Shaggy是的,这是一个近似值;但是+-这是打高尔夫球的人的正确选择
— RosLuP

根据我的计算,这里的分数是147个字节,而不是152个字节(字符数无关)。进一步阅读:codegolf.meta.stackexchange.com/q/9428/58974
— Shaggy

@Shaggy数字152将是一个仅包含该代码的文件的大小(以字节为单位)(复制过去,将该代码保存在记事本(窗口)文档中,并观察该文件的“属性”)
— RosLuP

那不是我们在这里计数字节的方式。
— 毛茸茸的
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.