减少因果分解的领导者变动


12

tl; dr:输出减少的素因数分解前导改变的值。

每个正整数都有一个唯一的素因式分解。让我们称简化素数分解为素数的多重性列表,按因子的大小排序。例如,减少的素因数分解1980[2, 2, 1, 1],因为1980 = 2 * 2 * 3 * 3 * 5 * 11

接下来,让我们记录下每个简化素数分解发生的频率[1, 2, ..., n]。例如,在中[1, 2, ..., 10],发生以下减少的素数分解:

[1]: 4 (2, 3, 5, 7)
[2]: 2 (4, 9)
[1, 1]: 2 (6, 10)
[]: 1 (1)
[3]: 1 (8)

我们将把领导者召集到n最经常发生的减少的素因分解[1, 2, ..., n]。因此,的简化素因分解导数n = 10[1]。关系将被小于或等于n减小的素因数分解的最大整数的大小破坏,较小的最大整数会更好。举例来说,最多n = 60的减少了黄金的因式分解[1][1, 1]出现每17倍。该范围内的最大整数[1, 1]58,而最大整数[1]59。因此,用n = 60减少的素因分解导数为[1, 1]

我对n简化素因数分解领导者发生变化的值感兴趣。这些是n精简因式分解领导者与精简因式分解领导者直到的差异的值n-1。作为一个极端的例子,我们可以说领导层在发生变化n = 1,因为领导层不存在n = 0

您的挑战是输出。

所需输出的初始序列为:

1, 3, 58, 61, 65, 73, 77, 1279789, 1280057, 1280066, 1280073, 1280437, 1280441, 1281155, 1281161, 1281165, 1281179, 1281190, 1281243, 1281247, 1281262, 1281271, 1281313, 1281365

允许的输出样式为:

  • 无限的输出。
  • 第一位k领导者发生变化,k输入在哪里。
  • k次领导人变化,其中k是输入。

k 可以是零或一个索引。

这是代码高尔夫球。如果您不确定任何事情,请在评论中提问。祝好运!


领导者的变化最多等于/小于k怎么办?
user202729 '18

@ user202729我要说不-这使挑战有些不同。
isaacg

由于您已经定义了正整数的概念,因此您可能希望允许人们以1或3开头序列。(或更改为“这些值n是归约素分解领导者与归素分解领导者不同的地方,直到n-1“)
乔纳森·艾伦

@JonathanAllan我没有改变任何事情,但是我澄清了挑战的相关部分。
isaacg

Answers:


3

外壳,18个字节

m←ġ(←►Lk=mȯmLgpṫ)N

在线尝试! 这将打印无限列表。链接将结果截断为前7个值,因为该程序效率很低,并且在TIO之后超时。

括号很难看,但我不知道该如何消除。

说明

m←ġ(←►Lk=mȯmLgpṫ)N  No input.
                 N  The list of natural numbers [1,2,3,4,..
  ġ(            )   Group into slices according to this function.
                    Each slice corresponds to a run of numbers with equal return values.
    ←►Lk=mȯmLgpṫ    Function: from n, compute the reduced factorization leader in [1..n].
                     As an example, take n = 12.
               ṫ     Reversed range: [12,11,10,9,8,7,6,5,4,3,2,1]
         m           Map over this range:
              p       Prime factorization: [[2,2,3],[11],[2,5],[3,3],[2,2,2],[7],[2,3],[5],[2,2],[3],[2],[]]
             g        Group equal elements: [[[2,2],[3]],[[11]],[[2],[5]],[[3,3]],[[2,2,2]],[[7]],[[2],[3]],[[5]],[[2,2]],[[3]],[[2]],[]]
          ȯmL         Take length of each run: [[2,1],[1],[1,1],[2],[3],[1],[1,1],[1],[2],[1],[1],[]]
       k=            Classify by equality: [[[2,1]],[[1],[1],[1],[1],[1]],[[1,1],[1,1]],[[2],[2]],[[3]],[[]]]
                     The classes are ordered by first occurrence.
     ►L              Take the class of maximal length: [[1],[1],[1],[1],[1]]
                     In case of a tie, ► prefers elements that occur later.
    ←                Take first element, which is the reduced factorization leader: [1]
                    The result of this grouping is [[1,2],[3,4,..,57],[58,59,60],[61,62,63,64],..
m←                  Get the first element of each group: [1,3,58,61,65,73,77,..

为什么不起作用►=。难道maxBy不喜欢后面的元素?
H.PWiz '18

@ H.PWiz的问题是,在平局的情况下,我需要更喜欢最大化的元素,其首先出现在反向范围是最新的可能,或者等价地,其去年的增长幅度出现是最早的可能。►=两者都不做。
Zgarb

1

JavaScript(ES6),120字节

返回第1个索引的第N个领导者变更。

N=>(F=m=>N?F((F[k=(D=(n,d=2,j)=>d>n?j:n%d?D(n,d+1)+(j?[,j]:[]):D(n/d,d,-~j))(++n)]=-~F[k])>m?F[N-=p!=k,p=k]:m):n)(n=p=0)

演示版

已评论

辅助函数D(),以相反的顺序返回n的简化素数分解:

D = (n, d = 2, j) =>             // n = input, d = divisor, j = counter
  d > n ?                        // if d is greater than n:
    j                            //   append j and stop recursion
  :                              // else:
    n % d ?                      //   if d is not a divisor of n:
      D(n, d + 1) + (            //     recursive call with n unchanged and d = d + 1
        j ?                      //     if j is not undefined:
          [,j]                   //       append a comma followed by j
        :                        //     else:
          []                     //       append nothing
      )                          //
    :                            //   else:
      D(n / d, d, -~j)           //     recursive call with n divided by d and j = j + 1

主功能:

N =>                             // N = target index in the sequence
  (F = m =>                      // m = # of times the leader has been encountered
    N ?                          // if N is not equal to 0:
      F(                         //   do a recursive call to F():
        (F[k = D(++n)] =         //     increment n; k = reduced prime factorization of n
                         -~F[k]) //     increment F[k] = # of times k has been encountered
        > m ?                    //     if the result is greater than m:
          F[N -= p != k,         //       decrement N if p is not equal to k
                         p = k]  //       update p and set m to F[p]
        :                        //     else:
          m                      //       let m unchanged
      )                          //   end of recursive call
    :                            // else:
      n                          //   stop recursion and return n
  )(n = p = 0)                   // initial call to F() with m = n = p = 0

1

Stax,24 个字节

Ç▓Δk/‼&²Θºk∙♥╜fv╛Pg8╝j♀§

该程序无需输入,理论上可以产生无限大的输出。我说“理论上”是因为第8个元素将花费一年多的时间。

运行并调试

同一程序的相应ascii表示法是这样的。

0WYi^{|n0-m|=c:uny=!*{i^Q}Md

它将最后一个领导者保留在堆栈中。迭代整数,如果因子表示中有一个独特的模式,并且与上一个不同,则将其输出。

0                               push zero for a placeholder factorization
 W                              repeat the rest of the program forever
  Y                             store the last factorization in the y register
   i^                           i+1 where i is the iteration index
     {    m                     using this block, map values [1 .. i+1]
      |n0-                          get the prime exponents, and remove zeroes 
           |=                   get all modes
             c:u                copy mode array and test if there's only one
                ny=!            test if mode array is not equal to last leader
                    *           multiply; this is a logical and
                     {   }M     if true, execute this block
                      i^Q       push i+1 and print without popping
                           d    discard the top of stack
                                    if it was a leader change, this pops i+1
                                    otherwise it pops the mode array
                                at this point, the last leader is on top of 
                                the stack

0

Python 2,145个字节

m=i=0;f=[]
while 1:
 i+=1;a=i;d=[0]*-~i;k=2
 while~-a:x=a%k>0;k+=x;a/=x or k;d[k]+=1-x
 k=filter(abs,d);f+=k,;c=f.count
 if c(k)>c(m):print i;m=k

在线尝试!

不打高尔夫球

m=0                    # reduced prime factorizations leader
i=0                    # current number
f=[]                   # list of reduced prime factorizations
while 1:               # Infinite loop:
  i+=1                 #   next number
  a=i                  #   a is used for the prime factorization
  d=[0]*-~i            #   this lists stores the multiplicity
  k=2                  #   current factor
  while~-a:            #   As long as a-1 != 0:
    x=a%k>0            #      x := not (k divides a)
    k+=x               #      If k does not divide a, go to the next factor
    a/=x or k          #      If k does not divide a,
                       #         divide a by 1,
                       #         else divide it by k
    d[k]+=1-x          #      add 1 to the multiplicity of k if k divides a
  k=filter(abs,d)      #   Only keep non-zero multiplicities
                       #     k is now the reduced prime factorization of i
  f+=k,                #   append k to the list of reduced prime factorizations
  c=f.count            #   c(x) := number of occurences of x in f
  if c(k)>c(m):        #   has the current reduced prime factorization
                       #    appeared more often than the leader?
    print i;m=k        #     print the current number, set new leader

在线尝试!


0

果冻 35  34 字节

我觉得它仍然可以打高尔夫球

ÆEḟ0µ€ĠL€M⁸’ߤ¹Ṗ?
®‘©Ç€F0;ITµL<³µ¿

一个完整的程序,用于获取k并输出第一个k领导者变更点的果冻列表表示。

在线尝试!

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.