每一段Mathemania代码都以数字2开头。在2中,您可以执行以下操作:
e:指数。此命令的默认设置是对数字进行平方。f:阶乘。此命令的默认值是使用数字上的单阶乘(using f on 2 = 2! = 2)。r:根。此命令的缺省值是平方根数字。c:天花板功能。l:地板功能。要在Mathemania中生成一个数字,必须将这些命令串在一起,这些命令在数字2上从左到右执行。
ef = (2^2)! = 4! = 24
rl = floor(sqrt(2)) = floor(1.4...) = 1
er = sqrt(2^2) = sqrt(4) = 2
efrrc = ceil(sqrt(sqrt((2^2)!)))
= ceil(sqrt(sqrt(24)))
= ceil(sqrt(4.89...))
= ceil(2.21...)
= 3e、f和r命令可以通过额外的Mathemania命令(它也以2作为其“基”数)进行修改,通过在修改后的函数后放置括号并在函数中放置Mathemania命令来生成不同的指数、阶乘和根。
例如,要裁剪一个数字而不是对其进行平方,您可以将3的命令放在e之后,如下所示:
e(efrrc) -> cube a number, "efrrc" = 3注意:就我们的目的而言,阶乘命令(f)以2作为一个单阶乘开始。因此,如果您执行f(efrrc)__,它将被评估为一个双阶乘,而不是一个三重阶乘。
对于n__-阶乘(例如双阶乘=2-阶乘,三次阶乘=3-阶乘等),基数乘以n小于它的数,n小于它,等等,直到最后一个数不能被n减去而不变成0或负数。
例如:
7!! = 7 * 5 * 3 * 1 = 105 (repeatedly subtract 2, 1 is the last term as
1 - 2 = -1, which is negative)
9!!! = 9 * 6 * 3 = 162 (repeatedly subtract 3, 3 is the last term as
3 - 3 = 0, which is 0)有关更多信息,请参见这里。
您可以在任何地方插入它,它将由Mathemania作为一个单一函数来处理:
e(efrrc)rc = ceil(sqrt(2^3))
= ceil(2.82...)
= 3你也可以把它们相互嵌套:
e(e(e)) = e(4th power)
= (2^4)th power
= 16th power要获得数学代码的解释器,请单击这里 (干杯,@BradGilbertb2gills!)
您的任务是创建一个程序,当给定一个正整数n作为输入时,该程序生成一个数学程序,在执行时返回n。
但是,您生成的Mathemania程序必须尽可能小(打高尔夫),并且您的最终得分取决于示例中生成的Mathemania程序中字节数的总和,这些字节数是10,000到10,100的整数。得分最低者获胜。
10,000和10,100之间的数字才会被测试。e、f和r,这些函数中的Mathemania代码(例如,e(efrrc),其中efrrc是函数中的代码)必须计算为2上面的正整数。如果您的程序不遵循这一规则,它也将被取消资格。5并输出efrc时,它必须在每次输入5时输出。这是元狼,所以最低分赢了!
发布于 2016-12-27 04:35:38
到目前为止,我还没有所有101个输入的输出,但是一旦我运行了所有测试用例的程序,我将用我的分数进行更新。
from math import *
memoized = {}
same = {}
def _(mathmania, n):
memoized[n] = mathmania
return mathmania
def is_prime(n):
if n == 2:
return True
if n % 2 == 0 or n <= 1:
return False
for divisor in range(3, int(sqrt(n)) + 1, 2):
if n % divisor == 0:
return False
return True
def pair_key(pair):
low, high = pair
diff = high - low
if diff == 0:
return 100
low_done, high_done, diff_done = low in memoized, high in memoized, diff in memoized
if high_done and memoized[high] == None or low_done and memoized[low] == None:
return -1
return (high_done + diff_done + (diff + 1 == low)) * 33 + low / high
def major_pairs(n):
for i in range(n, int(sqrt(n)), -1):
d = n / i
if i - d < d - 1:
break
if d == int(d):
yield (int(d), i)
def fact_key(pair):
i, f = pair
if i in memoized:
if memoized[i] == None:
return -1
return 1
return i / f
def near_fact(n, level):
s = 4
if n in same:
s = same[n]
for i in range(s, n ** 2 ** level):
f = factorial(i)
if f > (n - 1) ** 2 ** level:
if f < (n + 1) ** 2 ** level:
same[n] = i
yield (i, f)
else:
return
def generate_mathmania(n):
if n in memoized and memoized[n] != None:
return memoized[n]
memoized[n] = None
binx = log(n, 2)
if binx == int(binx):
if binx == 2:
return _("e", n)
if binx == 1:
return _("er", n)
if binx == 0:
return _("rl", n)
return _("e(" + generate_mathmania(int(binx)) + ")", n)
sq = sqrt(n)
if sq == int(sq):
return _(generate_mathmania(int(sq)) + "e", n)
low, high = max(major_pairs(n), key=pair_key)
if pair_key((low, high)) == -1:
level = 1
while True:
try:
i, f = max(near_fact(n, level), key=fact_key)
except:
level += 1
continue
if fact_key((i, f)) == -1:
return _(generate_mathmania((n - 1) ** 2 + 1) + "rc", n)
if f == n ** 2 ** level:
return _(generate_mathmania(i) + "f" + "r" * level, n)
if f < n ** 2 ** level:
return _(generate_mathmania(i) + "f" + "r" * level + "c", n)
return _(generate_mathmania(i) + "f" + "r" * level + "l", n)
if low != 1:
if low == high:
return _(generate_mathmania(low) + "e", n)
if high - low == 1:
return _(generate_mathmania(high) + "f", n)
return _(generate_mathmania(high) + "f(" + generate_mathmania(high - low + 1) + ")", n)
good = None
for i in range(n ** 2 - 1, (n - 1) ** 2, -1):
if i in memoized:
return _(generate_mathmania(i) + "rc", n)
if not is_prime(i):
good = i
if good:
return _(generate_mathmania(good) + "rc", n)
for i in range((n + 1) ** 2 - 1, n ** 2, -1):
if i in memoized:
return _(generate_mathmania(i) + "rl", n)
if not is_prime(i):
good = i
if good:
return _(generate_mathmania(good) + "rl", n)
return _(generate_mathmania((n - 1) ** 2 + 1), n)此外,由于数量庞大,我无法验证我尝试过的一些测试用例的输出,此时@BradGilbertb2gills的在线解释器超时。希望所有的输出都能工作。
https://codegolf.stackexchange.com/questions/104426
复制相似问题