Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the origin of magic number 42, indispensable in coding? [closed]

Tags:

Update:
Surprised that it is being so heavily downvoted...

The question is coding-related and before asking this question I have googled for "42" in combination with:

  • site:msdn.micrsoft.com
  • "code example"
  • "c#"
  • "magic number"

And I am not an expert/fan of Western culture/literature.

Also found, Why are variables “i” and “j” used for counters? [duplicate] which was not closed but even protected.


I feel that everybody knows it, except me...

What is the origin of ubiquitous magic digit 42 used all over the code samples and samples?

How have you come using 42? because I have not ever come or ever used 42

After some search, I found MSDN doc on it: Magic Numbers: Integers:

  • "Aside from a book/movie reference, developers often use this as an arbitrary value"

Well, this did not explain me anything.

Which movies and books have I missed for all those years of being involved in development, coding and programming and around-IT related activities like rwquirements analysis, system administration, etc??

Some references to some texts using code snippets with 42 (just C#-related):

Jérôme Laban. C# Async Tips and Tricks, Part 3: Tasks and the Synchronization Context

  var t = Task.Delay(TimeSpan.FromSeconds(1))               .ContinueWith                 (                     _ => Task.Delay(TimeSpan.FromSeconds(42))                 ); 

MSDN Asynchronous Agents Library

   send(_target, 42);   

Quickstart: Calling asynchronous APIs in C# or Visual Basic

  Office.context.document.setSelectedDataAsync(       "<html><body>hello world</body></html>",        {coercionType: "html", asyncContext: 42},        function(asyncResult) {            write(asyncResult.status + " " + asyncResult.asyncContext); 

Asynchronous Programming in C++ Using PPL

  task<int> myTask = someOtherTask.then([]() { return 42; }); 

Boxing and Unboxing (C# Programming Guide)

  Console.WriteLine(String.Concat("Answer", 42, true)); 

How To: Override the ToString Method (C# Programming Guide)

  int x = 42; 

Trace Listeners

  // Use this example when debugging.   System.Diagnostics.Debug.WriteLine("Error in Widget 42");   // Use this example when tracing.   System.Diagnostics.Trace.WriteLine("Error in Widget 42"); 

|| Operator (C# Reference

  // The following line displays True, because 42 is evenly     // divisible by 7.   Console.WriteLine("Divisible returns {0}.", Divisible(42, 7));    // The following line displays False, because 42 is not evenly    // divisible by 5.   Console.WriteLine("Divisible returns {0}.", Divisible(42, 5));    // The following line displays False when method Divisible     // uses ||, because you cannot divide by 0.    // If method Divisible uses | instead of ||, this line    // causes an exception.   Console.WriteLine("Divisible returns {0}.", Divisible(42, 0)); 

WIKIPedia C Sharp (programming language)

  int foo = 42;         // Value type. 
like image 843

People also ask

Why is 42 used in programming?

In ASCII language, the most basic computer software, '42′ is the designation for an asterisk. So, when Deep Thought was asked what the true meaning of life was, it answered as you might think a computer would - 42, in other words, “Anything you want it to be!”

What is a magic number in coding?

In programming, a magic number is a constant value used to identify a file format, protocol or error code. In many file formats, the first few bytes identify the file; for example, "PK" in ZIP files and the hex values "F8 D8" in JPEG files.

Why is the number 7 considered as a magic number in software engineering?

Miller repeated his tests across multiple categories of learning and processing, from vision, to hearing, to memory recall, and the results were unanimous: people appear to be able to process and recall between five and nine pieces of information at any one time. Hence, the magic number seven, plus or minus two.

What are magic numbers C++?

A magic number is a numeric literal that is used in the code without any explanation of its meaning. The use of magic numbers makes programs less readable and hence more difficult to maintain and update.


2 Answers

It's from The Hitch Hiker's Guide to the Galaxy.

In The Hitchhiker's Guide to the Galaxy (published in 1979), the characters visit the legendary planet Magrathea, home to the now-collapsed planet-building industry, and meet Slartibartfast, a planetary coastline designer who was responsible for the fjords of Norway. Through archival recordings, he relates the story of a race of hyper-intelligent pan-dimensional beings who built a computer named Deep Thought to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. When the answer was revealed to be 42, Deep Thought explained that the answer was incomprehensible because the beings didn't know what they were asking. It went on to predict that another computer, more powerful than itself would be made and designed by it to calculate the question for the answer. (Later on, referencing this, Adams would create the 42 Puzzle, a puzzle which could be approached in multiple ways, all yielding the answer 42.)

like image 101
Andomar Avatar answered Sep 22 '22 14:09

Andomar


The answer is, as people already have stated, The Hitchhiker's Guide to the Galaxy.

I made a little experiment and put a couple of numbers in the search field, and these are the results:

enter image description here

It seems like 42 beats its neighbors clearly, but it can't touch regular numbers like 40, 45 and 50, no matter how magical it is.

It would be interesting to do the same search in source code only.

like image 26
Victor Sand Avatar answered Sep 18 '22 14:09

Victor Sand