编写一个数学函数,其结果为“ hello world”。


9

编写数学函数,得出“ hello world”的整数表示

例如,以十六进制表示的“ hello world”就是68656c6c6f20776f726c64这样的目标,那就是编写一个数学程序,生成一个表示“ hello world”的数字。

这是使用python的可能解决方案的示例:

print(2*2*3*59*61*97*416510539*72330832279)==int("68656c6c6f20776f726c64", 16)

可以使用任何类型的数学方程式,例如:幂,阶数,阶乘和其他数学运算。

规则:

  • 您可以选择自己的方式将“ hello world”编码/表示为整数。还允许使用哈希函数

  • 允许使用数学库(例如numpy,GMP)。

  • 目的是专注于数学部分


1
创造力不是客观的。您是否在考虑人气竞赛
Kendall Frey 2014年

4
如果允许您决定编码,则任何整数都可以表示“ hello world”。例如-1 = “你好worlc”,0 = “世界你好”,1 = “你好worle”等
肯德尔弗雷

1
@kylek由于您没有指定“最有创意/最英勇”之外的确切获胜标准,因此我自由地将人气竞赛添加为获胜标准。
Victor Stafusa 2014年

2
(在本网站的上下文中)所有计算机数据和程序都可以用数字简单地表示-因为这是发明人(巴贝奇等人)使用的模型。同样允许任何编码实际上只是一场竞赛,争夺谁可以编写产生数字的最具创造力的程序。暗示源代码应使用“数学运算”,但是程序所做的任何事情都可以建模为数学运算。建议您要求使用ASCII编码,然后进行竞赛以将其表示为“特殊数字”,请参阅:www2.stetson.edu/~efriedma/numbers.html
2014年

1
您在使用数学术语时玩得很快而且很随意。您似乎在交替使用“等式”和“功能”。我认为您要寻找的词是“表情”。但是即使那样,这个问题也不是很好。我为我选择的“ hello world”的数学编码是原始的K&R hello world源代码。哦,嘿,行得通!
Tim Seguine 2014年

Answers:


14

Python 2.7

对随机数生成器的某些滥用在Python 2.7上有效,但在Python 3.x上无效,因为该生成器似乎已更改了播种算法;

>>> import random
>>> n=[(15,30,15,25,15,0,-15,-25,-15,-30,-15),(107,35,34,26,22,0,71,188,94,64,81)]
>>> random.seed(4711)
>>> m = zip("MERRY CHRISTMAS",*n)
>>> print(''.join(map(lambda x:chr(ord(x[0])+random.randint(x[1],x[2])),m)))

hello world

这使我在办公桌前大笑。人们现在都在盯着我:-/
jmiserez 2014年

25

我会做得比只打印要好,我会无限次打印!

有理数

1767707668033969 / 3656158440062975

返回以下以36为基数的扩展:

0.helloworldhelloworldhelloworldhelloworldhelloworld...

试试看!(Wolfram Alpha)

或者,如果您想获得更深入的信息,请尝试:

2399843759207982499621172523113947162810942763812298565948669
/ 1357602166130257152481187563160405662935023615

哪个返回(再次以36为底):

helloworld.helpimtrappedinanumberfactoryhelpimtrappedinanumberfactoryhelpimtrappedinanumberfactory...

如果您的程序仅使用整数,那么您将永远不会看到小数部分。


12

Python (尚未完成...!)已完成!:D

number = ((sum(b'This text will end up converting to the text "hello world" by a bunch of math.') *
sum(b'For example, multiplication will be used to increase the number so that it may reach the required result.') *
sum(b'Wow, double multiplication! The required result number is extremely big so I have to use lots of math to get there.') *
sum(b'TRIPLE multiplication?!?!?! Wow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Did I put too many exclamation points?') *
sum(b'I think maybe I might have put too many exclamation points, oops. :(') *
sum(b'This is taking a long time...') *
sum(b'A very, very, very long time...')) // 2)

number -= (sum(b'Okay, the number is a bit too high. Blah. This is extremely hard.') *
sum(b'I need to reduce it. I will just keep multiplying again! Yay! That seems effective!') *
sum(b'I don\'t know what to write now... I used up all my creativity in the previous number.') *
sum(b'So. Uhhh, how has your day been? Was it good? I wonder how many people are still reading.') *
sum(b'Gah! There is nothing to type about anymore! I will just type randomness then.') *
sum(b'Do you like pie? I like pie. Refrigerator. The chicken ate the potato. Llamas are not tasty.'))

number -= (sum(b'Blah, I am still a tiny bit too high!') *
sum(b'This is very frustrating!') * sum(b'Argh!!!!!!!') *
sum(b'I have even less creative ideas now since I have written two paragraphs already.') *
sum(b'Well, I suppose they\'re not paragraphs. They\'re just blocks of code.') *
sum(b'The only reason I made that remark was to increase this number to subtract, so that I reach my target.'))

number -= (sum(b'I am an extremely miniscule amount over the target!!!') *
sum(b'I am so close!!! So close!!') *
sum(b'I must make it!!! I will!!!') *
sum(b'I shall NEVER give up!!!') *
sum(b'Okay, ummm... maybe not exactly "never"...') *
sum(b'I mean, this is REALLY hard...'))

number -= (sum(b'I am so close. This is just ridiculous.') *
sum(b'Just ridiculous. And that\'s awesome :D') *
sum(b'Why am I doing this?!?') *
sum(b'The answer\'s probably "becase I can."') *
sum(b'Notice how most of the text in this program is meaningless.'))

number -= (sum(b'How have I been above my target this whole time? That is very odd.') *
sum(b'I wonder how much time I could have saved if I removed a few characters in the first block.') *
sum(b'I wish I did that. That would have made it so much easier.... But oh well.') *
sum(b'Well, I am really really really really really really close now!'))

number -= (sum(b'I am so close!') *
sum(b'I will never give up now! Not after all this!') *
sum(b'I wonder if I will ever get exactly on the target. What if I do, and then discover a typo? :O') *
sum(b'So close!'))

number -= (sum(b'Wow; this is the eighth block. That\'s a lot of blocks!') *
sum(b'I only have a little more to go! I must finish! I will!') *
sum(b'It is starting to get harder thinking of things to type than it is getting to the target...'))

number -= (sum(b'These strings are short') *
sum(b'So the number to subtract is less') *
sum(b'Hi'))

number += (sum(b'Finally adding') *
sum(b'That\'s new') *
sum(b':D'))

number -= (sum(b'I am back to subtraction, and also longer strings.') *
sum(b'But this time I only get two strings!'))

number -= (sum(b'I have switched over to adding the strings, not multiplying them!') +
sum(b'I am really really close! So close that I can\'t even multiply two strings to subtract anymore!') +
sum(b'This is very exciting; I\'ve nearly made it! Only a few more strings to add...') +
sum(b'I ran out of ideas for what to type again... I will just type exactly what I am thinking, which is what I am doing now actually.') +
sum(b'But now the only thing I am thinking about is getting to the target, and there is not much about that to type...') +
sum(b'I am only a few thousand away!!!!!!!!!!!!'))

number += 8 # NOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

print(number)

输出126207244316550804821666916(相当于您的示例0x68656c6c6f20776f726c64


您对我的要求很了解。
kyle k 2014年

1
骆驼不好吃 ”哈哈+1
亚述2014年

2
这应该赢。
tomsmeding

8

您可以选择自己的方式将“ hello world”编码/表示为整数。

好吧...

的PHP

<?=1?>

其中1代表“ hello world”,因为我是这样说的。


14
10在碱基的“hello world”
周华健Vikström

7

计算器字体/ FreePascal

发布此邮件可能会给我带来麻烦,但是我还是会这样做的:-)

  1. 计算7734/100000。将前导零更改为蓝色,将其他数字更改为计算器字体。颠倒过来。蓝色的O代表世界:

在此处输入图片说明

如果太懒了,该怎么办:

uses graph;
var gd, gm : integer;
var n,m:integer;

begin
  gd := D4bit;
  gm := m640x480;
  initgraph(gd,gm,'');
  setcolor(black);

  For n:=0 to 5  do for m:=0 to 3 do begin;
    setfillstyle(solidfill,(abs(n*2-1)));

    if(($967EEF shr (20-n*4+m)) and 1) = 1
      then sector(50+n*80,100,90*m,90*(m+1),40,60);

    if(($EDF7E9 shr (20-n*4+m)) and 1) = 1
      then sector(50+n*80,300,90*m,90*(m+1),40,60);
    end;

  readln;
  closegraph;
end.

在此处输入图片说明

编号为0-5的圆圈,颜色由abs(2n-1)确定。每圈一个十六进制数字,从最高有效位开始,从右下方开始按顺时针方向上色。由神秘常数967EEF和EDF7E9组成。

特别感谢以下原语,并感谢Borland将其放入TurboPascal中,以便FreePascal可以克隆它。

procedure Sector(x:SmallInt; y:SmallInt; StAngle:Word; EndAngle:Word; XRadius:Word; YRadius:Word); 扇形以中心(X,Y)以及半径XRadius和YRadius绘制并填充一个椭圆形的扇形,从角度Start开始,在角度Stop结束。


我以前没有想过这种方法,开箱即用,好思考!
kyle k

3

Ruby和Python

使用基于36的数学,我们很容易在Ruby和Python中得出整数表示形式:


红宝石

%w(hello world).map{|n|n.to_i(36)}

结果:

[29234652, 54903217]

或表示为一般功能:

def f(words); words.split.map{|n|n.to_i(36)}; end

例:

f("hello world")
=> [29234652, 54903217]

蟒蛇

def f(words): return map(lambda n: int(n,36), words.split())

例:

>>> f("hello world")
[29234652, 54903217]

3

一些精心制作的PHP:

$x=18306744;
$w=($z=($y=30)/3)/2;
echo base_convert($x, $z, $y+$z/$w),chr($y+$z/$w).base_convert($x*($y/$z)-$w*41*83,$z,$y+$y/$w);

这是一个很好的答案,这是我一直在寻找的答案。
kyle k 2014年

1

R6RS计划

#!r6rs
(import (rnrs))
(define (hello-world)
  (bitwise-xor (fold-left (lambda (acc d) 
                        (+ (* acc 256) 
                           (+ (bitwise-and 255 acc) d)))
                      104
                      '(-3 7 0 3 8 0 -8 3 -6 -8))
               (bitwise-arithmetic-shift 87 40)))

输出#x68656c6c6f20776f726c64(十进制):

126207244316550804821666916

我最初的实现是:

球拍(方案方言)

(define (hello-world)
  (bitwise-xor (foldl (lambda (d acc) 
                        (+ (* acc 256) 
                           (+ (bitwise-and 255 acc) d)))
                      104
                      '(-3 7 0 3 8 0 -8 3 -6 -8))
               (arithmetic-shift 87 40)))

1

的JavaScript

function stringTheory(theory) {
  var proof = 0;
  var principles = theory.split(/[ ,.'-]/);
  for (var i = 0; i < principles.length; i++) {
      var formula = '';
      for (var j = 0; j < principles[i].length; j++) {
        formula += principles[i].charCodeAt(j).toString(10);
      }
      proof += +formula;
  }
  return proof;
}

console.log(
/* \2 and \3 are start of text and end of text characters */ 
stringTheory('\2 Yo it\'s 4327 - Go to space, look back, and see the dot of a small blue rock you once sat on amid the vast empty void - KA-BOOM - you are in awe of it. "Ah" - so tiny in this vast space yet you are even more so. A mere atom in an ocean of stars, the earth a speck of dust to the sun\'s ping-pong ball. One day you shall go back and as your toes touch the soft soil once more, the cool wind in your hair as you cast your gaze upon the moon, a mere rock just like this one, and bask in it\'s warm glow - Ah. Only then can you know the scale of it all, what luck you have to call this place home.  And with this new ken, a love you\'ve kept for all of time but had not seen - for it is clear to you now. You lay open your arms and fill the air with your song - (aah) ~o Good-bye space and ... o? \3') + 42
);

到底是怎么回事?

我们使用此字符串并应用一些stringTheory()(实际上是将来的传输):

'\2 Yo it\'s 4327 - Go to space, look back, and see the dot of a small blue rock you once sat on amid the vast empty void - KA-BOOM - you are in awe of it. "Ah" - so tiny in this vast space yet you are even more so. A mere atom in an ocean of stars, the earth a speck of dust to the sun\'s ping-pong ball. One day you shall go back and as your toes touch the soft soil once more, the cool wind in your hair as you cast your gaze upon the moon, a mere rock just like this one, and bask in it\'s warm glow - Ah. Only then can you know the scale of it all, what luck you have to call this place home. And with this new ken, a love you\'ve kept for all of time but had not seen - for it is clear to you now. You lay open your arms and fill the air with your song - (aah) ~o Good-bye space and ... o? \3'

首先,我们将其标点符号拆分成单词。然后,我们通过将字符转换为十进制ASCII码来创建一组数字。相邻的字母变为相邻的数字(例如,aa变为9797)。

然后将数字相加。我们得到的是191212222216169一个完全无用的数字,它没有任何意义,就像在太空中闲散地漂浮着的四千万块岩石一样。是什么让这个世界与众不同?为什么是生活。因此,通过给这个数字赋予生活意义, +=42我们就会得到191212222216211;

但为什么?

这是什么意思?为什么它stringTheory("Hello world")当然意味着。


1

蟒蛇

from math import fsum
c=[104.,-4412.705557362921,12008.518259002305,-13041.051140948179,7566.060243625142,-2619.91695720304,567.427662301322,-77.52280096313,6.48776455347,-0.303552138602,0.006079144624]
def f(x):
    return fsum([c[i]*x**i for i in range(len(c))])
s=""
for i in range(11):
    s+=chr(int(round(f(i))))
print s

0

红宝石

任何字符串都映射到一个整数。

# encode string to integer
def str_to_int(s)
  i = 0
  s.chars.each do |c|
    i = i << 7 | c.ord
  end
  i
end

反函数:

# decode integer to string
def int_to_str(i)
  s = ''
  while i > 0 do 
    s = (i & 0x7f).chr + s
    i = i >> 7
  end
  s
end

例子:

str_to_int("ABC")
=> 1073475
int_to_str(1073475)
=> "ABC"

str_to_int("hello world")
=> 123720932638399026476644
int_to_str(123720932638399026476644)
=> "hello world"

0

C#

一种将任何字符串(Hello World或其他形式)转换为十六进制字符串的方法

string Encode(string text)
{
   string hexValue = String.Empty;
   foreach(char c in text)
      hexValue += String.Format("{0:X}", (int)c);
   return hexValue;
}

将十六进制字符串转换为字符串的方法

string Decode(string hexValue)
{
   string text = String.Empty;
   for (int i = 0; i < hexValue.Length; i += 2)
   {
     int value = Convert.ToInt32(hexValue.Substring(i, 2), 16);
     text += (char)value;
   }
   return text;
}

这是完整的C#程序

using System;

namespace ConsoleApplication1
{
    class Program
    {       

        static string Encode(string text)
        {
            string hexValue = String.Empty;
            foreach(char c in text)
                hexValue += String.Format("{0:X}", (int)c);
            return hexValue;
        }

        static string Decode(string hexValue)
        {
            string text = String.Empty;

            for (int i = 0; i < hexValue.Length; i += 2)
            {
                int value = Convert.ToInt32(hexValue.Substring(i, 2), 16);
                text += (char)value;
            }

            return text;
        }

        static void Main(string[] args)
        {  
            string Text1 = "Hello World!";
            Console.WriteLine("Before Encoding: " + Text1 + "\n");

            string Hex = Encode(Text1);
            Console.WriteLine("After endoding: " + Hex + "\n");

            string Text2 = Decode(Hex);
            Console.WriteLine("After decoding: " + Text2 + "\n");
        }
    }
}

这是输出。

在此处输入图片说明


0

蟒蛇

def D(A):
    try:
        return sum(D(x)**2 for x in A)
    except TypeError:
        return A
print hex(D([[[[33,22,3,1],[20,13,2],5],[[31,19,1],[11,5,3],1]],[[[26,13],[18,14,6,4],1],[[12,10],[10,3,1],2],4],28]))

将目标数递归分解为最多四个平方的和。我的基本情况是<100。我使用了http://www.alpertron.com.ar/FSQUARES.HTM来计算分解。

(也许<= 1的基本情况会很有趣...)


0

蟒蛇

def church(i):
    if i > 0:
        return 'f(' + church(i-1) + ')'
    else:
        return 'x'

def church_string(bs):
    import base64
    return church(int(base64.b16encode(bs), 16))

print(church_string(b'hello world'))

我发誓,结果的确是一个数字!


0

只是C带数字的乐趣,没什么特别的。

int main()
{
    int a , b, c;
    double i = 0, f = 0;

    for (; i < 1; i += .00000012785666)
        f += cos(i);
    c = f;
    a = f * 276.393089;
    b = a + f * 7.4358109;

    printf("%.4s%.4s%.4s\n", &a, &b, &c);
}

演示


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.