Write a mathematical function whose result is "hello world"

9

2

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

kyle k

Posted 2014-01-04T19:47:12.513

Reputation: 409

Question was closed 2016-04-25T14:23:53.753

What is the winning criterion? – Victor Stafusa – 2014-01-04T19:50:03.137

@Victor The most creative/ingenious equation – kyle k – 2014-01-04T19:50:50.970

1Creativity is not objective. Are you thinking of [tag:popularity-contest]? – Kendall Frey – 2014-01-04T19:51:37.350

4Any 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. – Kendall Frey – 2014-01-04T19:52:17.683

@KendallFrey I added an answer exploiting what you are saying just to show that. – Victor Stafusa – 2014-01-04T20:41:10.503

1@kylek Since you did not specified the exact winning criteria beyond "the most creative/ingenious", i took the liberty to add the [tag:popularity-contest] as the winning criterion. – Victor Stafusa – 2014-01-04T20:42:25.820

2

(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: http://www2.stetson.edu/~efriedma/numbers.html

– user2460798 – 2014-01-04T21:05:12.417

Shouldn't the function by which a number is produced by "hello world" be invertible, such that, if you receive the number and apply the inverse, "hello world" is output? – DavidC – 2014-01-04T21:14:26.687

@DavidCarraher yes it should – kyle k – 2014-01-04T21:19:02.253

1You 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! – Tim Seguine – 2014-01-04T22:26:55.317

@Tim I am also a little bit confused about the proper way to describe my question. – kyle k – 2014-01-04T22:29:05.243

1

Here's a guy who wrote an actual mathematical formula where f(0)='H', f(1)='e' etc. http://www.stavros.io/posts/printing-hello-world-using-curve-fitting/

– marinus – 2014-01-06T01:02:55.843

Answers

14

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

Joachim Isaksson

Posted 2014-01-04T19:47:12.513

Reputation: 1 161

This made me laugh out loud at my desk. People are staring at me right now :-/ – jmiserez – 2014-08-02T18:22:02.220

25

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.

Joe Z.

Posted 2014-01-04T19:47:12.513

Reputation: 30 589

12

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)

Doorknob

Posted 2014-01-04T19:47:12.513

Reputation: 68 138

You have a good understand of what I am asking. – kyle k – 2014-01-04T20:45:16.567

1"Llamas are not tasty" haha +1 – assylias – 2014-01-06T23:14:56.467

2This should win. – tomsmeding – 2014-01-17T22:42:26.473

8

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.

Tortoise

Posted 2014-01-04T19:47:12.513

Reputation: 224

1410 in base-"hello world" – Emil Vikström – 2014-01-05T16:44:55.703

7

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.

Level River St

Posted 2014-01-04T19:47:12.513

Reputation: 22 049

I have not thought of this method before, Good thinking out of the box! – kyle k – 2014-02-02T03:02:31.460

3

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]

Darren Stone

Posted 2014-01-04T19:47:12.513

Reputation: 5 072

3

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

nitro2k01

Posted 2014-01-04T19:47:12.513

Reputation: 1 283

This is a good answer, It is the kind of answer I was looking for. – kyle k – 2014-01-04T21:12:08.750

1

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

Sylwester

Posted 2014-01-04T19:47:12.513

Reputation: 3 678

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

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.

George Reith

Posted 2014-01-04T19:47:12.513

Reputation: 2 424

1

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

KSFT

Posted 2014-01-04T19:47:12.513

Reputation: 1 527

0

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"

Darren Stone

Posted 2014-01-04T19:47:12.513

Reputation: 5 072

0

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

Merin Nakarmi

Posted 2014-01-04T19:47:12.513

Reputation: 247

0

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

Keith Randall

Posted 2014-01-04T19:47:12.513

Reputation: 19 865

0

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!

JAB

Posted 2014-01-04T19:47:12.513

Reputation: 181

0

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

David

Posted 2014-01-04T19:47:12.513

Reputation: 141

-2

(+ 110145154154157040127157162154144 0)

user13544

Posted 2014-01-04T19:47:12.513

Reputation: 1

1This is Lisp, I take it? – Jwosty – 2014-03-02T02:56:32.297