Sequentia Filii Bonacci


14

比萨的莱昂纳多(约1175年-约1245年)以斐波那契闻名。但这实际上是18世纪(根据维基百科)组成的拉丁语“ filius Bonacci”(Bonacci的儿子)的缩写。)。

在此挑战中,您将获得一个介于120之间的序数(按字面意义),并且您必须返回斐波那契数列中的相应项。

所不同的是,序数将以拉丁文给出。

例如:“ duodecimus”→ 89

完整的I / O表

 input              | meaning | output
--------------------+---------+--------
 "primus"           |   1st   |    0
 "secundus"         |   2nd   |    1
 "tertius"          |   3rd   |    1
 "quartus"          |   4th   |    2
 "quintus"          |   5th   |    3
 "sextus"           |   6th   |    5
 "septimus"         |   7th   |    8
 "octavus"          |   8th   |   13
 "nonus"            |   9th   |   21
 "decimus"          |  10th   |   34
 "undecimus"        |  11th   |   55
 "duodecimus"       |  12th   |   89
 "tertius decimus"  |  13th   |  144
 "quartus decimus"  |  14th   |  233
 "quintus decimus"  |  15th   |  377
 "sextus decimus"   |  16th   |  610
 "septimus decimus" |  17th   |  987
 "duodevicesimus"   |  18th   | 1597
 "undevicesimus"    |  19th   | 2584
 "vicesimus"        |  20th   | 4181

规则

  • 输入保证是 完全是上述字符串之一。
  • 如果有帮助,则可以将其全部大写。但是,所有条目都必须保持一致。不允许混合使用。
  • 根据您的算法和语言,硬编码或计算序列项可能会赢或输字节。明确允许两种方法。
  • 这是

有趣的事实:这里有一个latin.stackexchange.com
JayCe,

Answers:


8

R91 86字节

在强力的UTF8查找表中查找字节总和的索引,并使用神奇的Fibonacci生成函数给出答案。

function(x)round(1.618^match(sum(!x)%%93,!'%(-1!5+3JOSCW6')*.2765)
"!"=utf8ToInt

在线尝试!

编辑:-2字节通过改进的数字舍入

编辑:-3字节,更改了查找(感谢提示@Giuseppe!)


想要与UTF8查找相似。没有线索可以让斐波那契那样。大概比我想做的事(短chartr总和的UTF8与斐波那契值的UTF8,然后intToUtf8输出chartr
JayCe


"sextus decimus"朱塞佩似乎失败了。
J.Doe

1
您在正确的地方@Giuseppe,原来有3个不可思议的2位模数,其字节的总和是唯一的,分别为69、88和93,而88是不需要添加任何常量的字节。可理解的字符串。
J.Doe

9
有时我觉得代码高尔夫球的一半正在寻找正确的数字用作模数……
Giuseppe

4

Ruby,104 93字节

->x{[8,4181,3,144,21,13,0,1,233,5,987,0,377,55,0,89,1,1597,34,610,0,2,2584][x.sum%192%76%23]}

在线尝试!

只需将字节的总和取模192模76模23,然后索引到查找表中。(通过强力发现的魔术数字。)


4

干净,87字节

\n由于编译器可以使用实际的原始值,所以除转义之外的所有转义都将被视为一个字节。(TIO和SE存在一些问题,尽管它不是有效的UTF-8,所以这里转义了)

FryAmTheEggman做了一个不错的演示/解决方法:这里

import StdEnv
$s=hd[i\\i<-k 1 0&c<-:"\340\152\060\065\071\354\172\045\223\n\255\362\132\137\143\026\244\051\344\270"|c==sum s]
k b a=[a:k(a+b)b]

在线尝试!

定义函数$ :: [Char] -> Int,该函数使用大写字符值的总和的唯一性来确定k要返回序列中的哪个项(由helper函数生成)。


4

6502机器代码(C64),82字节

00 C0 20 9E AD 20 A3 B6 A8 88 A9 05 4A 90 02 49 B1 71 22 88 10 F6 29 1F C9 07
B0 02 69 0D A8 BE 32 C0 B9 1E C0 4C CD BD 00 00 00 00 03 00 0A 00 06 10 01 00
FF 00 02 00 00 00 00 00 08 00 15 0D DB 02 18 90 3D 55 79 05 FF E9 62 22 01 59
01 37 FF 03

(当然)这使用了散列,但是针对在6502上的短实施进行了优化,并利用了移位设置的进位标志并另外使用。通过使用一些C程序进行蛮力搜索可以找到用于散列的幻数。的FF字节是哈希表中的不幸漏洞;)

字节数:2个字节的加载地址,38个字节的代码,42个字节的哈希表以获取值。

在线演示

用法:SYS49152"[ordinal]"例如SYS49152"DUODECIMUS"。(请注意,字母在默认的C64配置中显示为大写)。

重要说明:在第一次启动之前,请发出NEW命令。这是必须的,因为C64 BASIC LOAD命令会摆弄一些BASIC向量,即使在将机器程序加载到某个绝对地址(例如此处$C000/49152)。

已评论拆解

         00 C0                          ; load address
.C:c000  20 9E AD    JSR $AD9E          ; evaluate expression
.C:c003  20 A3 B6    JSR $B6A3          ; evaluate as string
.C:c006  A8          TAY                ; length to y register
.C:c007  88          DEY                ; decrement (start at last char)
.C:c008  A9 05       LDA #$05           ; start value for hash
.C:c00a   .hashloop:
.C:c00a  4A          LSR A              ; shift right
.C:c00b  90 02       BCC .skip          ; shifted bit zero? -> skip xor
.C:c00d  49 B1       EOR #$B1           ; xor "magic" value
.C:c00f   .skip:
.C:c00f  71 22       ADC ($22),Y        ; add current character (plus carry)
.C:c011  88          DEY                ; previous character
.C:c012  10 F6       BPL .hashloop      ; pos >= 0? -> repeat
.C:c014  29 1F       AND #$1F           ; mask lowest 5 bits
.C:c016  C9 07       CMP #$07           ; larger than 7 ?
.C:c018  B0 02       BCS .output        ; -> to output
.C:c01a  69 0D       ADC #$0D           ; add 13
.C:c01c   .output:
.C:c01c  A8          TAY                ; hash to y register
.C:c01d  BE 32 C0    LDX .lb-8,Y        ; load low byte from hashtable
.C:c020  B9 1E C0    LDA .hb-8,Y        ; load high byte from hashtable
.C:c023  4C CD BD    JMP $BDCD          ; to output of 16bit number
.C:c026   .hb:
.C:c026  00 00 00 00 .BYTE $00,$00,$00,$00
.C:c02a  03 00 0A 00 .BYTE $03,$00,$0A,$00
.C:c02e  06 10 01 00 .BYTE $06,$10,$01,$00
.C:c032  FF 00 02 00 .BYTE $FF,$00,$02,$00
.C:c036  00 00 00 00 .BYTE $00,$00,$00,$00
.C:c03a   .lb:
.C:c03a  08 00 15 0D .BYTE $08,$00,$15,$0D  ; second byte used in .hb as well
.C:c03e  DB 02 18 90 .BYTE $DB,$02,$18,$90
.C:c042  3D 55 79 05 .BYTE $3D,$55,$79,$05
.C:c046  FF E9 62 22 .BYTE $FF,$E9,$62,$22
.C:c04a  01 59 01 37 .BYTE $01,$59,$01,$37
.C:c04e  FF 03       .BYTE $FF,$03

C64 BASIC V2测试套件

(在行中包含机器程序DATA

在线演示

0fOa=49152to49231:rEb:pOa,b:nE
1?"primus",:sY49152"primus":?
2?"secundus",:sY49152"secundus":?
3?"tertius",:sY49152"tertius":?
4?"quartus",:sY49152"quartus":?
5?"quintus",:sY49152"quintus":?
6?"sextus",:sY49152"sextus":?
7?"septimus",:sY49152"septimus":?
8?"octavus",:sY49152"octavus":?
9?"nonus",:sY49152"nonus":?
10?"decimus",:sY49152"decimus":?
11?"undecimus",:sY49152"undecimus":?
12?"duodecimus",:sY49152"duodecimus":?
13?"tertius decimus",:sY49152"tertius decimus":?
14?"quartus decimus",:sY49152"quartus decimus":?
15?"quintus decimus",:sY49152"quintus decimus":?
16?"sextus decimus",:sY49152"sextus decimus":?
17?"septimus decimus",:sY49152"septimus decimus":?
18?"duodevicesimus",:sY49152"duodevicesimus":?
19?"undevicesimus",:sY49152"undevicesimus":?
20?"vicesimus",:sY49152"vicesimus":?
21dA32,158,173,32,163,182,168,136,169,5,74,144,2,73,177,113,34,136,16,246,41,31
22dA201,7,176,2,105,13,168,190,50,192,185,30,192,76,205,189,0,0,0,0,3,0,10,0,6
23dA16,1,0,255,0,2,0,0,0,0,0,8,0,21,13,219,2,24,144,61,85,121,5,255,233,98,34,1
24dA89,1,55,255,3

3

Perl 6,62个字节

{(0,1,*+*...*)[index '%(-1!5+3JOSCW6',chr .ords.sum%93]}

在线尝试!

在字符串中使用查找表,以及简短的斐波那契序列生成器。


3

C(gcc)135 129字节

根据ceilingcat和Logern的建议减少6个字节

f;i;b;o;n(char*a){for(f=i=b=o=0;*a;o+=21*b+++*a++-70);for(b=1,o="TACIHAAJQFSAAARCAMGKDANPEAOAAL"[o%30];--o>65;f=b,b=i)i=f+b;a=i;}

在线尝试!

说明:

f; i; b; o; // Variables

n (char *a)
{
     // Calculate a silly hash of incoming string
     for (f = i = b = o = 0; *a; o += 21 * b++ + *a++ - 70);

     // Use the hash to index into the array of number values
     // and calculate the corresponding Fibonacci sequence element
     for
     (
         b = 1, 
         o = "TACIHAAJQFSAAARCAMGKDANPEAOAAL"[o % 30]; 

         --o > 65; 
         f = b, b = i
     )
         i = f + b;

     // implicit return
     a = i;
}

您可以替换return i;a=i;
Logern 18'Sep

2

Pyth,54个字节

L?>b1+ytbyttbbyxc."axnÛ±r†XVW‹(„WîµÏ£"2+hQ@Q618

测试套件

注意:由于代码使用了一些不可打印的字符,因此在Stack Exchange上可能无法正确显示。提供的链接指向可以正常工作且可复制的源。

长话短说,Q[0]+Q[618%len(Q)]为所有接受的输入提供独特的结果Q


1

Python 2,292字节

f=lambda x:x>1and f(x-1)+f(x-2)or x
def g(t):
	for i,c in zip("pr secun ter qua qui sex sep oc no ec".split(),range(1,11))+zip("un duo es ev".split(),(1,2,20,"-")):t=t.replace(i,str(c))
	return f(abs(eval("+".join("".join((" ",c)[c in"0123456789-"]for c in t).split()).replace("+-+","-")))-1)

在线尝试!

斐波那契发电机从这个答案中被无耻地偷走了。

将每个单词分解成有意义的组成部分,并丢弃其余部分(例如,在“ duodevicesimus”中,我们只关心“ duo ev es”->“ 2-20”-> abs(“ 2-20”)-> 18)。

将计算出的值(负1到0偏移)传递给Fibonacci生成器函数。

取消说明:

# Fibonacci function
f=lambda x:x>1and f(x-1)+f(x-2)or x

def g(t):
    # generates a list of key, value pairs like [("pr", 1, ..., ("ec", 10)] +
    values = zip("pr secun ter qua qui sex sep oc no ec".split(), range(1,11))

    # adds values to existing list
    values += zip("un duo es ev".split(),(1,2,20,"-"))

    # replace the matching values in the string with the appropriate number/sign.
    # ORDER MATTERS - if "un" is searched for before "secun", this generates incorrect values.
    for i,c in values:
        t = t.replace(i,str(c))

    # replace each non-digit, non-minus character in string with "c"
    t = [(" ",c)[c in"0123456789-"]for c in t]

    # convert space-replaced array back to a string
    # then split it on spaces, creating an array of values
    t = "".join(t).split()

    # turn the array back into a string, with each individual item separated by "+"
    # this will cause "-" to become "+-+" (since "-" is ALWAYS between two numbers), so prelace that sequence with "-"
    t = "+".join(t).replace("+-+","-")

    # evaluate the string as an expression, and take the absolute value for when - occurs
    t = abs(eval(t))

    # pass the value, minus 1 for 0-offset, to the Fibonacci function.
    return f(t-1)

1

Python 2中97 79个字节

lambda s:int(1.618**('RV3NJ^[KDP=Z62AWeG<C'.find(chr(hash(s)%69+48)))*.4474+.5)

在线尝试!

首先,我们要从拉丁转换为数字n;这可以通过复制输入字符串足够多次来确保总共至少11个字符来完成。然后第3rd 10个字符和第th个字符(零索引)形成一个唯一的对,并使用哈希mod 69,并将其转换为可打印的char。

现在我们有了n。要找到n第Fibonacci数,我们可以使用舍入方法,仅使用所需的位数精确到Fib(20)。


1

JavaScript(Node.js)100 97 95 92 91字节

x=>1.618**(p=parseInt)("1 jgf7  ei 490dbch62385a"[p(x.length+x,32)%12789%24],36)*.4474+.5|0

在线尝试!

警告:由于浮动点不准确而导致的工作

JavaScript既没有内置的哈希函数,也没有足够简短的字符到ASCII函数(String.charCodeAt已经是最短的)了,所以我需要自己定义一个简单的哈希函数。

计算哈希后,使用与Chas Brown相同的舍入方法。

经过一整天的残酷强迫,发现了一个更好的方法:

b32_to_dec(x.length + x) % 12789 % 24 (*浮点数不准确)

b32_to_dec(x.length + x) % 353 % 27 (*浮点数不准确)

b36_to_dec(x.length + x[2]) % 158 % 29 - 4

b36_to_dec(x[2] + x.length) % 741 % 30

b36_to_dec(x[0] + x[2] + x.length) % 4190 % 27

parseInt(x.length + x, 32) 结果

primus 7310236636
secundus 9773632960476
tertius 272155724764
quartus 269453490140
quintus 269461747676
sextus 7054
septimus 9774067964892
octavus 266721394652
nonus 192700380
decimus 254959770588
undecimus 350449413217244
duodecimus 36520018280274912 **NOT PRECISE**
tertius decimus 1302947875804
quartus decimus 1300245641180
quintus decimus 1300253898716
sextus decimus 37774
septimus decimus 42759416798172
duodevicesimus 43016381192006637977600 **NOT PRECISE**
undevicesimus 1326703556626028691456 **NOT PRECISE**
vicesimus 351376069188572

不使用浮点精度的版本:95字节

x=>1.618**(p=parseInt)("52d7i 6  he 8309jafc 41bg"[p(x.length+x[2],36)%158%29-4],36)*.4474+.5|0

在线尝试!

b36_to_dec(x.length + x[2]) % 158 % 29 - 4

哈希表

 Latin word           | Length | 3rd | Hash | Decimal | %158 | %29-4
----------------------+--------+-----+------+---------+------+-------
 primus               |      6 | i   | 6i   |     234 |   76 |    14
 secundus             |      8 | c   | 8c   |     300 |  142 |    22
 tertius              |      7 | r   | 7r   |     279 |  121 |     1
 quartus              |      7 | a   | 7a   |     262 |  104 |    13
 quintus              |      7 | i   | 7i   |     270 |  112 |    21
 sextus               |      6 | x   | 6x   |     249 |   91 |     0
 septimus             |      8 | p   | 8p   |     313 |  155 |     6
 octavus              |      7 | t   | 7t   |     281 |  123 |     3
 nonus                |      5 | n   | 5n   |     203 |   45 |    12
 decimus              |      7 | c   | 7c   |     264 |  106 |    15
 undecimus            |      9 | d   | 9d   |     337 |   21 |    17
 duodecimus           |     10 | o   | 10o  |    1320 |   56 |    23
 tertius decimus      |     15 | r   | 15r  |    1503 |   81 |    19
 quartus decimus      |     15 | a   | 15a  |    1486 |   64 |     2
 quintus decimus      |     15 | i   | 15i  |    1494 |   72 |    10
 sextus decimus       |     14 | x   | 14x  |    1473 |   51 |    18
 septimus decimus     |     16 | p   | 16p  |    1537 |  115 |    24
 duodevicesimus       |     14 | o   | 14o  |    1464 |   42 |     9
 undevicesimus        |     13 | d   | 13d  |    1417 |  153 |     4
 vicesimus            |      9 | c   | 9c   |     336 |   20 |    16
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.