382
votes
\$\begingroup\$

Douglas Adams was born on March 11, 1952, and died when he was just 49. In honor of this wonderful writer, I challenge you to display 42 in the most creative way possible.

You could print it in the log, via some convoluted method, or display it as ASCII art, or anything! Just come up with a creative method of displaying 42.

Because this a popularity-contest, whichever answer has the most upvotes by March 11, will be declared the winner.

Note: this is not a duplicate. The question it was marked as duplicating was a code-trolling question whose goal was to write code to output 42, not find the most creative way to display it.

Winner: grovesNL! With an astounding 813 votes! Congrats!

Honorable Mentions:

Mr Lister C 228 For the clever use of #define

David Carraher Mathematica 45 For the complicated and convoluted math function to achieve 42

Aschratt Windows Calculator 20 Because, well, it's windows calculator And definitely 1337.

f.rodrigues Python 17 Because of the clever use of using external programs. And MSPaint

Jason C LMGTFY 14 For the use of LMGTFY (Let Me Google That For You)

Trimsty Python 12 For the clever use of an error message to output 42.

Mukul Kumar C++ 7 For the nice ASCII output.

If you think that there is another answer worth putting on the list, please comment it!

\$\endgroup\$
22
  • 6
    \$\begingroup\$ The old closed question was a code-trolling, i.e, anything that does not shows 42 beside appearing to do so or do it in a really horrible way. This one is not code-trolling, i.e. the output should really be 42 in a nice way. Hence it is not duplicate. \$\endgroup\$ Feb 23, 2014 at 22:59
  • 2
    \$\begingroup\$ @DanDascalescu This SE site has many questions without strict requirements, which allows users to exercise creative freedom in their answers. Sometimes too many restrictions can impede the expression of creativity \$\endgroup\$
    – grovesNL
    Feb 24, 2014 at 0:45
  • 9
    \$\begingroup\$ The question should remain at 42 votes. \$\endgroup\$
    – Zero Fiber
    Feb 24, 2014 at 3:47
  • 28
    \$\begingroup\$ Marvin The Robot "I would post my brilliant answer, but you would probably down vote it. Being right all the time is so depressing." \$\endgroup\$
    – Reactgular
    Feb 24, 2014 at 16:43
  • 3
    \$\begingroup\$ Can we get 42 favorites? \$\endgroup\$
    – Milo
    Feb 24, 2014 at 22:48

110 Answers 110

1 2 3
4
2
votes
\$\begingroup\$

Non-Alphanumeric JavaScript

[][(__=''+!!(_=+[]))[_$=-~-~-~_]+($$=''+{})[$=-~_]+($_=''+!_)[$]+$_[_]][$$[-~($+_$)]+$$[$]+(''+$/_)[$]+__[_$]+$_[_]+$_[$]+$_[$+$]+$$[-~($+_$)]+$_[_]+$$[$]+$_[$]](__[$]+__[$+$]+$_[_$]+$_[$]+$_[_]+"("+((($<<($<<$)^$)<<($<<$)^$)<<$)+")")()

The real secret is in ((($<<($<<$)^$)<<($<<$)^$)<<$) which does the real dirty work in generating the 42 from bitwise operators based off a single variable, $ which is worth 1.

\$\endgroup\$
2
votes
\$\begingroup\$

C

#include <stdio.h>

int main()
{
  int answer = 6*9;
  char* wanted;
  for (wanted = "answer"; *wanted; wanted++)
    answer ^= *wanted;
  printf("%d\n", answer);
  return 0;
}
\$\endgroup\$
2
votes
\$\begingroup\$

k4

  */.:'(-;*;^)
42

Sometimes, k is a language only a Vogon could love....

\$\endgroup\$
2
votes
\$\begingroup\$

Now that I know that desmos is a valid language... at least, for these purposes.

:D

View the result here!

You also may notice that it only uses (asides from the instance of 10, an instance of 1 and the three instances of e) the numerals 4 and 2.

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

PYTHON

import webbrowser
webbrowser.open('http://t2.gstatic.com/images?q=tbn:ANd9GcTBdE69EouKgYcnWjjFko7Jx2gBgTVLu-n-tIn3QSu8EHhOJ_GsTxSwGVfD')
\$\endgroup\$
1
vote
\$\begingroup\$

C#

Most everyone is connected in our daily lives. How about using the internet? Also, we judge web pages by their titles, so hopefully there is something useful in there.

using System;
using System.Net;

public class Universe {
 public static void Main(String[] args) {
    Byte[] EncodedAnswerPage = new Byte[] { 104,116,116,112,115,58,47,47,99,111,100,101,103,111,108,102,46,115,116,97,99,107,101,120,99,104,97,110,103,101,46,99,111,109,47,113,117,101,115,116,105,111,110,115,47,50,49,56,51,53,47,109,111,115,116,45,99,114,101,97,116,105,118,101,45,119,97,121,45,116,111,45,100,105,115,112,108,97,121,45,52,50 };
    WebClient Client = new WebClient();
    String s;
    try
    {
        s = Client.DownloadString(System.Text.Encoding.ASCII.GetString(EncodedAnswerPage)).Replace("\r\n", "");
    }
    catch (WebException)
    {
        Console.WriteLine("Sorry, I have failed you!!! I cannot give you the answer to life, the universe and everything.");
        Environment.Exit(42);
        return;
    }
    Console.WriteLine("The answer to life, the universe and everything is...");
    System.Threading.Thread.Sleep(2000);
    Console.WriteLine("6 * 7, which equals " + s[42 + 50] + "" + s[42 + 51]);
    Console.ReadLine();
            Environment.Exit(42);
 }
}
\$\endgroup\$
1
vote
\$\begingroup\$

Javascript:

console.log("The answer to life, the universe, and everything is elite pie.");
console.log(Math.round(Math.PI * 13.37));
\$\endgroup\$
1
vote
\$\begingroup\$

Ruby

puts "the answer to life, the universe, and everything: ".tap{|n|$><<n}.split.map(&:ord).inject(-2){|a,b|a+b*(-1)**b}
\$\endgroup\$
1
vote
\$\begingroup\$

PHP

Going really short with this... ord() will return ASCII value as an int for * character which is 42

<?=ord('*'); //Outputs 42

Demo

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

My faithful represntation of Deep Thought's great computation.

Haskell:

import Data.Char
import Math.NumberTheory.Primes
import System.IO

main =    putStrLn message
     >>   hSetBuffering stdin NoBuffering
     >>   getChar
     >>= (putStrLn . process)
  where
    process xs
      | confirm xs = (++) " [>]: " . show $ compute xs
      | otherwise  = failure
    message =  "\n [!] Press ENTER to confirm computation of the following query: \n"
            ++ "\n [?] Answer to the Ultimate Question of Life, the Universe, and Everything"
    failure =  "\n [X] Confirmation failed, computation aborted"
    confirm = (=='\n')
    compute = succ . sum . flip takeWhile primes . flip (<=)
            . (+) ((!!) primes . fromIntegral $ head primes)
            . fromIntegral . ord

Note that this may out-perform Deep Thought's original computation on your machine. If so, it's only because Deep Thought lacked a version of GHC which targeted it's architecture natively.

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

Obligatory Golfscript solutions:

"Answer To Life The Universe And Everything",

"The Hitchhiker's Guide to the Galaxy"{+}*
"Douglas Noel Adams"{+}*
-`{42-}%{+}*)

"DA"{64-}%{}/)
\$\endgroup\$
1
  • \$\begingroup\$ The first one is the best! \$\endgroup\$
    – minseong
    Apr 2, 2015 at 20:06
1
vote
\$\begingroup\$

Cartesian Function

Graph this on a coordinate plane.

((4x+9)sqrt(|.25-|x+1.75||/(.25-|x+1.75|))-y)((4x+5)sqrt(|.5-|x+1||/(.5-|x+1|))-y)(sqrt(|.75-|x+1.25||/(.75-|x+1.25|))-y)(3sqrt(|.5-|x-.5||/(.5-|x-.5|))-y)((4x-1)sqrt(|.25-|x-.75||/(.25-|x-.75|))-y)(2xsqrt(|.5-|x||/(.5-|x|))-y)(-sqrt(|.5-|x||/(.5-|x|))-y)=0
\$\endgroup\$
1
vote
\$\begingroup\$

Ti-84 BASIC

6*9→rand:randInt(1,42+42+42+422)
\$\endgroup\$
1
vote
\$\begingroup\$

TI-BASIC

Gah, I've had this forever waiting for a place to use it... it's pretty much exactly the same as the top answer, though. Whatever, here it is!

1.25→F
F-.01(ᴇ2fPart(F→O
6.4(.01(ᴇ2fPart(F→R
tanh(Fᴇ9→T
F17.6→Y

√((2.96296296296296)(1.35→W

Disp FORTY-TWO⁻
\$\endgroup\$
1
vote
\$\begingroup\$

JavaScript

It's a canoe race! Who will win?

 ~     ~     ~   ~ ~(~!~[]*~[]*~4)+  ~
  ~  +[] +[] +[]+    ~  ~    ~ ~
   ~ ~   ~!~[[[]]]*~[]-~[]+  ~  ~ +[~0]+~~1
\$\endgroup\$
1
vote
\$\begingroup\$

Here's another one:

JAVA

import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.util.Random;

import javax.swing.JFrame;

public class FortyTwo {
    static Random r=new Random();
    static String pickOne(String[]s){
        return s[r.nextInt(s.length)];
    }
    public static void main(String[]a){
        double pi=3;
        double count=0;
        long in=0;
        String[]one={
                "Analyzing",
                "Bloxing",
                "Copying",
                "Hypnotizing",
                "Importing",
                "Indexing",
                "Initializing",
                "Loading",
                "Locating",
                "Polarizing",
                "Refactoring",
                "Resizing",
                "Scanning",
                "Synchronizing",
                "Updating"
        };
        String[]two={
                "Altairian",
                "Bistromathic",
                "Hooloovoo",
                "Hyperwave",
                "Logical",
                "Magrathean",
                "Optical",
                "Prismatic",
                "Probability",
                "Quantum",
                "Sub-Etha",
                "Tangential"
        };
        String[]three={
                "Circuit",
                "Dictionary",
                "Drive",
                "Engine",
                "Grid",
                "Mainframe",
                "Matrix",
                "Library",
                "Loop",
                "Utilities"
        };
        System.out.print(">>");
        try {
            Thread.sleep(200+r.nextInt(100));
        } catch (InterruptedException e) {}
        for(char c:"What is the answer to the ultimate question about life, the universe, and everything?".toCharArray()){
            try {
                Thread.sleep(70+r.nextInt(30));
            } catch (InterruptedException e) {}
            System.out.print(c);
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {}
        System.out.println();System.out.println();
        System.out.print(pickOne(one)+" "+pickOne(two)+" "+pickOne(three)+"... ");
        final int tot=500+r.nextInt(200);
        int l=0;
        int q=300+r.nextInt(800);
        int z=15+r.nextInt(5);
        long t=System.currentTimeMillis()+q;
        while(l<tot){
            in+=Math.pow(Math.random(),2)+Math.pow(Math.random(),2)<1?1:0;
            boolean S=Math.random()<0.012;
            count++;
            pi=(pi+4*in/count)/2;
            if(System.currentTimeMillis()>t){
                z--;
                System.out.println(S?"FAILED":"Done.");
                if(S){
                    try {
                        Thread.sleep(100+r.nextInt(50));
                    } catch (InterruptedException e) {}
                    System.out.println("Attempting alternate route.");
                    try {
                        Thread.sleep(500+r.nextInt(80));
                    } catch (InterruptedException e) {}
                    if(z==0)z+=r.nextInt(3)+1;
                }
                if(z==0){
                    z=20+r.nextInt(10);
                    int percent=(1000*l)/tot;
                    System.out.println((percent/10)+"."+(percent%10)+"% complete.");
                }
                System.out.print(pickOne(one)+" "+pickOne(two)+" "+pickOne(three)+"... ");
                if(!S)l+=q/300;
                t+=q=300+r.nextInt(800);
            }

        }
        System.out.println("Done.");
        System.out.println("=CALCULATIONS COMPLETE=");
        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("The answer to the ultimate question about life, the universe, and everything is:");
        frame.setVisible(true);
        frame.setBounds(frame.getX(),frame.getY(),600,150);
        TextComponent c=new TextArea();
        c.setText(String.valueOf((int)(13.37*pi)));
        c.setBounds(0,0,600,100);
        c.setFont(new Font("Verdana",Font.PLAIN,96));
        frame.add(c);
    }
}

Prints randomly generated technobabble (at present, there are 1,800 possible phrases) while performing a Monte Carlo approximation of pi, then creates a window with the floor(13.37*pi)=42 as its contents

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

Matlab

This function asks Wolfram Alpha "what is the meaning of life?", returns the HTML of the page to a string, then finds and outputs the input interpretation and result.

d=urlread('http://www.wolframalpha.com/input/?i=what+is+the+meaning+of+life%3F');
k=strfind(d,'"stringified": "');
q=strfind(d,'","mInput":');
disp(['Input interpretation: ' d(k(1)+16:q(1)-1)])
fprintf(['Result: ' d(k(2)+16:q(2)-1) '\n'])

Output:

Input interpretation: Answer to the Ultimate Question of Life, the Universe, and Everything

Result: 42

(according to Douglas Adams' humorous science-fiction novel The Hitchhiker's Guide to the Galaxy)

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

><>

I made a bunch of these, so here they are.

1111111111111++++++$+++++++*n; 42$~:42$/42$~*+*:42$/42$~*+*n; !;3!na!+*!+9!3| 'douglas adams'42$nn; 42*:1+*n;

Oh, and then there's this one.

0aa**3a*2++o0aa**3a*2++o0aa**5a*1++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**9a*7++o0aa**9a*2++oao0aa**3a*2++o0aa**4a*7++o0aa**4a*3++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**9a*2++oao0aa**4a*7++o0aa**3a*2++o0aa**4a*8++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**3a*2++o0aa**4a*2++oao1aa**1a*0++o0aa**4a*7++o1aa**2a*4++o0aa**4a*5++o0aa**3a*2++o0aa**3a*2++o0aa**5a*9++oao0aa**3a*2++o0aa**3a*2++o0aa**9a*2++o0aa**3a*2++o0aa**3a*2++o0aa**4a*3++o0aa**5a*7++o0aa**4a*7++o0aa**9a*2++oao;

It prints 42 in ascii art, and if you run it as ><> again, you get 42.

Try it all here.

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

Java

public class Hello42 {
static int i=8;

static public enum Enum42{
    ONE(),TWO();
    Enum42(){
        System.out.print(i/=2);
    }
}

public static void main(String[] args) {
     Enum42.values();
}
}
\$\endgroup\$
-11
votes
\$\begingroup\$

PHP

echo '42';

Sorry, not sorry.

All you haters, this was meant to be sarcastic! But then again, sarcasm isn't allowed here.

\$\endgroup\$
2
  • \$\begingroup\$ So your answer got 3 downvotes and you deleted it :) I don't think this is a very creative option. \$\endgroup\$
    – Cilan
    Mar 1, 2014 at 18:25
  • 1
    \$\begingroup\$ Nope, that was about as uncreative as it gets. Challenge u to beat that. \$\endgroup\$
    – geoff
    Mar 1, 2014 at 22:59
1 2 3
4

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