首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >素数笔画笔画

素数笔画笔画
EN

Code Golf用户
提问于 2023-04-23 17:17:32
回答 3查看 1.3K关注 0票数 -4

挑战

您应该输出我最近设计的系列,如下所示:升素数的笔画计数:

代码语言:javascript
复制
2, 3, 2, 4, 3, 5, 6, 5, 7, 7, 7, 10, 4, 6, 7, 4, 4, 4, 7, 6, 8...

示例

这说明了这个系列是如何形成的,首先,它从序列形式中获取一个素数,所以它使用第一个素数2。它将其转换为2的罗马数字,即II,这里笔画是一条直线,在本例中是2,因此本系列中的第一个元素是2

字典

解释每一个字母的笔画都会令人费解,而且我们知道所有的罗马数字都只包含I, V, X, L, C, D, M字符,这里已经显示了每个字母的笔画值。

代码语言:javascript
复制
0  C
1  I, L, D
2  V, X
3  [None]
4  M

例如,MMMMMMMCMXIX是罗马数字7919,所以您可以将它与上面的字典进行比较,M有4种笔画,等等,它们添加到37笔画中。

歧义

可以询问为什么M没有被指定为2笔画,而L没有被分配2次笔画;这是因为它们不是以数字的方式编写的。正如M和L所写的:

在标准罗马数字中,M作4笔笔画,L作1,因为L的另一行太小,不能认为是笔画。

任务

以获得输入号的字节数编写最短的代码,并从输入中输出尽可能多的元素。

测试用例

代码语言:javascript
复制
5 => 2, 3, 2, 4, 3
10 => 2, 3, 2, 4, 3, 5, 6, 5, 7, 7
15 => 2, 3, 2, 4, 3, 5, 6, 5, 7, 7, 7, 10, 4, 6, 7

不要忘记,这是实现笔画数的罗马数字的素数,只有升序!

EN

回答 3

Code Golf用户

发布于 2023-04-23 19:14:00

因子 + math.primes math.unicode roman,63字节

代码语言:javascript
复制
[ nprimes [ >roman [ "c--ildvx----m"index 3 /i ] map Σ ] map ]

在网上试试!

代码语言:javascript
复制
[                            ! start quotation (anonymous function)
    nprimes                  ! get a list of the first number of primes indicated by input
    [                        ! start map
        >roman               ! convert current prime to roman numerals
        [                    ! start map
            "c--ildvx----m"  ! push string to stack
            index            ! find the index of the current letter in the string
            3 /i             ! integer divide by 3                
        ] map                ! map over each letter in the roman numerals of current prime
        Σ                    ! take sum of results of the letters
    ] map                    ! map over the first n primes
]                            ! end quotation
票数 5
EN

Code Golf用户

发布于 2023-04-24 07:44:23

05AB1E,14 字节数

代码语言:javascript
复制
Åpε.XÇŽœ
s%5%O

JonathanAllan的Vyxal回答港,所以一定要投他一票!

在网上试试。

输出没有输入的无限序列的

也是14 字节数

代码语言:javascript
复制
Žœ
∞<Ø.X€Ç%5%O

在网上试试。

Explanation:

代码语言:javascript
复制
Åp               # Get a list of the first (implicit) input amount of primes
  ε              # Map over each prime number:
   .X            #  Convert it to a Roman number string
     Ç           #  Convert this string to a list of its codepoint integers
      Žœ\n       #  Push compressed integer 39602
          s      #  Swap so the list of lists of codepoint integers is at the top
           %     #  Modulo the 39602 by each of these codepoints
            5%   #  Modulo-5 that
              O  #  Take the sum of each inner list
                 # (after which the resulting list is output implicitly)

看这个05AB1E我的尖端(部分)如何压缩大整数?)来理解为什么Žœ\n39602

票数 3
EN

Code Golf用户

发布于 2023-04-25 09:44:47

JavaScript (ES6),155个字节

用所有不相关的东西完整地回答。

代码语言:javascript
复制
f=(k,n=1)=>k?(g=d=>n%d--?g(d):d)(n++)?f(k,n):["2345545673"[n%10]-(g=d=>n/d%10%9<4)(10)-g(100)+11%~(q=-~n/10%5)*2+(q-4?4*!q:6)+(n/1e3+.1<<2),...f(k-1,n)]:[]

在网上试试!

核心任务(96字节)

下面是一个更容易测试的有趣部分:一个函数,它使用整数并返回它的罗马数字表示中笔画的笔画数。

代码语言:javascript
复制
n=>"2345545673"[n%10]-(g=d=>n/d%10%9<4)(10)-g(100)+11%~(q=-~n/10%5)*2+(q-4?4*!q:6)+(n/1e3+.1<<2)

在网上试试!

评论

代码语言:javascript
复制
n =>           // n = input
"2345545673"   // hard-coded number of straight strokes + 2 in I's
[n % 10] -     // and V's, which can be computed modulo 10
(              // g is a helper function processing L's and D's,
  g = d =>     // which follow the same logic:
  n / d % 10   //   divide n by the argument and reduce modulo 10
  % 9          //   further reduce modulo 9
  < 4          //   is the result less than 4?
)(10) -        // first call to g with d = 10
g(100) +       // 2nd call to g with d = 100
11 %           // the number of X's can be computed modulo 50
~(             // we first evaluate the generic expression:
  q =          //   11 mod floor(q + 1)
  -~n / 10 % 5 //   with q = ((n + 1) / 10) mod 5
) * 2 +        // and double the result to get the number of strokes
(              // we then apply two corrections:
  q - 4 ?      //
    4 * !q     //   +4 strokes if q = 0, i.e. n = 49 (mod 50)
  :            //
    6          //   +6 strokes if q = 4, i.e. n = 39 (mod 50)
) +            //
(              //
  n / 1e3 + .1 // the number of M's is floor(n / 1000 + 1 / 10)
  << 2         // we multiply by 4 to get the number of strokes
)              //
票数 0
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/260393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档