9
\$\begingroup\$

Write a mathematical function that results in an integer representation of "hello world"

For example "hello world" in hex is 68656c6c6f20776f726c64 so the goal will be to write a mathematical program that results in a number representing "hello world".

Here is a example of a possible solution using python:

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

Any type of mathematical equations could be used, such as: powers, series, factorials, and other mathematical operations.

The rules:

  • You may select your own way to encode/represent "hello world" as an integer. Hash functions are also allowed

  • Mathematical libraries (e.g. numpy, GMP) are allowed.

  • The intention is to focus on the mathematics part

\$\endgroup\$

closed as off-topic by cat, Mego, Peter Taylor, Blue, Rɪᴋᴇʀ Apr 25 '16 at 14:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – cat, Mego, Blue, Rɪᴋᴇʀ
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Creativity is not objective. Are you thinking of popularity-contest? \$\endgroup\$ – Kendall Frey Jan 4 '14 at 19:51
  • 4
    \$\begingroup\$ Any integer can represent "hello world" if you are allowed to decide the encoding. e.g. -1 = "hello worlc", 0 = "hello world", 1 = "hello worle", etc. \$\endgroup\$ – Kendall Frey Jan 4 '14 at 19:52
  • 1
    \$\begingroup\$ @kylek Since you did not specified the exact winning criteria beyond "the most creative/ingenious", i took the liberty to add the popularity-contest as the winning criterion. \$\endgroup\$ – Victor Stafusa Jan 4 '14 at 20:42
  • 2
    \$\begingroup\$ (in the context of this website) all computer data and programs can be trivially represented as numbers - because that's the model the inventors (Babbage, et al) used. Also allowing any encoding really just makes this a contest of who can write the most creative program that yields a number. There is an implication that the source code should use "mathematical operations", but anything a program does can be modeled as a math op. Suggest you require ASCII encoding and then make the contest about representing it as a "special number" See: www2.stetson.edu/~efriedma/numbers.html \$\endgroup\$ – user2460798 Jan 4 '14 at 21:05
  • 1
    \$\begingroup\$ You are playing extremely fast and loose with your mathematical terminology. You are using "equation" and "function" seemingly interchangeably. I think the word you were looking for is "expression". But even then, the question is not well posed. The mathematical encoding for "hello world" I choose for mine is the original K&R hello world source code. Oh, hey, it works! \$\endgroup\$ – Tim Seguine Jan 4 '14 at 22:26

16 Answers 16

14
\$\begingroup\$

Python 2.7

Some abuse of the random number generator, works on Python 2.7 but not Python 3.x since the generator seems to have changed the seeding algorithm;

>>> 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
\$\endgroup\$
  • \$\begingroup\$ This made me laugh out loud at my desk. People are staring at me right now :-/ \$\endgroup\$ – jmiserez Aug 2 '14 at 18:22
25
\$\begingroup\$

I'll do better than just printing it, I'll print it infinitely many times!

The rational number

1767707668033969 / 3656158440062975

returns the following base-36 expansion:

0.helloworldhelloworldhelloworldhelloworldhelloworld...

Try it out! (Wolfram Alpha)

Or, if you want a more subliminal message, try:

2399843759207982499621172523113947162810942763812298565948669
/ 1357602166130257152481187563160405662935023615

Which returns (again in base 36):

helloworld.helpimtrappedinanumberfactoryhelpimtrappedinanumberfactoryhelpimtrappedinanumberfactory...

If your program worked with only integers, you'd never see the fractional part.

\$\endgroup\$
12
\$\begingroup\$

Python (not finished... yet!) finished! :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)

Outputs 126207244316550804821666916 (equivalent to your example, 0x68656c6c6f20776f726c64)

\$\endgroup\$
  • \$\begingroup\$ You have a good understand of what I am asking. \$\endgroup\$ – kyle k Jan 4 '14 at 20:45
  • 1
    \$\begingroup\$ "Llamas are not tasty" haha +1 \$\endgroup\$ – assylias Jan 6 '14 at 23:14
  • 2
    \$\begingroup\$ This should win. \$\endgroup\$ – tomsmeding Jan 17 '14 at 22:42
8
\$\begingroup\$

You may select your own way to encode/represent "hello world" as an integer.

Well, then...

PHP

<?=1?>

where 1 represents "hello world" because I said so.

\$\endgroup\$
  • 14
    \$\begingroup\$ 10 in base-"hello world" \$\endgroup\$ – Emil Vikström Jan 5 '14 at 16:44
7
\$\begingroup\$

Calculator Font / FreePascal

I may get into trouble for posting this, but I'll do it anyway :-)

  1. Calculate 7734/100000. Change the leading zero to blue, and the other digits to calculator font. Turn it upside down. The blue O represents the world:

enter image description here

If that's too lazy, how about this:

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.

enter image description here

circles numbered 0-5, colour determined by abs(2n-1). One hex digit per circle, quadrants coloured from most significant bit downwards, clockwise from bottom right. By the mystic constants 967EEF and EDF7E9 hex.

Special thanks to the following primitive, and to Borland for putting it in TurboPascal so that FreePascal could clone it.

procedure Sector( x: SmallInt; y: SmallInt; StAngle: Word; EndAngle: Word; XRadius: Word; YRadius: Word ); Sector draws and fills a sector of an ellipse with center (X,Y) and radii XRadius and YRadius, starting at angle Start and ending at angle Stop.

\$\endgroup\$
  • \$\begingroup\$ I have not thought of this method before, Good thinking out of the box! \$\endgroup\$ – kyle k Feb 2 '14 at 3:02
3
\$\begingroup\$

Ruby & Python

Using base 36 math, we arrive at an integer representation quite easily in Ruby & Python:


Ruby

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

result:

[29234652, 54903217]

or, expressed as a general function:

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

example:

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

Python

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

example:

>>> f("hello world")
[29234652, 54903217]
\$\endgroup\$
3
\$\begingroup\$

Some carefully crafted 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);
\$\endgroup\$
  • \$\begingroup\$ This is a good answer, It is the kind of answer I was looking for. \$\endgroup\$ – kyle k Jan 4 '14 at 21:12
1
\$\begingroup\$

R6RS Scheme

#!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)))

Outputs #x68656c6c6f20776f726c64 (in decemal):

126207244316550804821666916

My original implementation was:

Racket (Scheme dialect)

(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)))
\$\endgroup\$
1
\$\begingroup\$

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
);

What is going on?

We take this string and apply a little stringTheory() (it is actually a transmission from the future):

'\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'

First we split it at its punctuation to form words. We then create a set of numbers by converting the characters to their decimal ASCII code. Adjoined letters become adjoined numbers (e.g. aa becomes 9797).

The numbers are then summed. What we get back is 191212222216169 an utterly useless number, it has no meaning, much like the quadrillions of rocks that float idly in space. What makes this world special? Why it is life. So by giving this number the meaning of life +=42 then we get 191212222216211;

But why?

What does this mean? Why it means stringTheory("Hello world") of course.

\$\endgroup\$
1
\$\begingroup\$

Python

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
\$\endgroup\$
0
\$\begingroup\$

Ruby

Any string is mapped to an integer.

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

The inverse function:

# 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

Examples:

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

str_to_int("hello world")
=> 123720932638399026476644
int_to_str(123720932638399026476644)
=> "hello world"
\$\endgroup\$
0
\$\begingroup\$

C#

A method to convert any string (hello world or something else) to hexadecimal string

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

A method to convert hexadecimal string to string

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;
}

Here goes the complete C# program

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");
        }
    }
}

And here is the output.

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Python

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]))

Recursively decomposes the target number into sums at most four squares. My base case is <100. I used http://www.alpertron.com.ar/FSQUARES.HTM to calculate the decompositions.

(Maybe a base case of <=1 would have been interesting...)

\$\endgroup\$
0
\$\begingroup\$

Python

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'))

The result really is a number, I swear!

\$\endgroup\$
0
\$\begingroup\$

Just a little C fun with numbers, nothing special.

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);
}

demo

\$\endgroup\$
-2
\$\begingroup\$
(+ 110145154154157040127157162154144 0)
\$\endgroup\$
  • 1
    \$\begingroup\$ This is Lisp, I take it? \$\endgroup\$ – Jwosty Mar 2 '14 at 2:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.