# Print E=mc² or E=mc^2, by using the right side of this equation

Your task – if you choose to accept it – is to have your program print, literally, either one of the following:

1. E=mc² (if your language supports Unicode (so no HTML trickery, for instance))

2. E=mc^2 (if your language doesn't support Unicode)

Here are the conditions:

• Your program must consist of exactly two expressions (one representing the m, the other the c, in mc²), which, when fed into your language's representation of the formula mc², and whose result is then printed, will show one of the above results.

• Both expression may be assigned to variables before feeding them into mc², but the expressions themselves are not allowed to use intermediate variables.

• Both expressions must evaluate to an absolute value larger than 1 (e.g abs(expression) > 1).

• You may assign the result of mc² to an intermediate variable, representing the E, before printing this E, but that is not necessary – you may print the result of mc² directly as well.

• When printing the final result, you are allowed to use your language's libraries to convert the possibly numeric or binary result to it's string representation.

• Added condition: no operator overloading in the final expression that represents mc². The result must be the algebraic result of expression m times expression c squared.

Here's an example template, of what I'm looking for, expressed in PHP:

$m = /* some expression */;$c = /* some expression */;
$E =$m * pow( $c, 2 ); print$E; // should print on of the results mentioned at the beginning of this challenge


To be honest, I'm not even sure this challenge is doable, but my gut says it must be possible in at least one, or even a few, languages.

Winning solution is the one with the shortest code, in characters, in a week from now.

For clarification, it's perfectly fine to merely do something like this as well — if your language allows for it:

print( someExpression * someOtherExpression ** 2 ); // or something to that effect


... in other words: without assigning anything to any intermediate variables. Just as long as the algebraic calculation remains intact.

• This seems like it would be extremely easy to do with operator overloading – Hannesh Feb 19 '14 at 15:47
• @Hannesh Hmm, yeah, I think I'll disallow operator overloading; at least in the final expression representing mc² (if it's even at all possible to be that selective with operator overloading). – Decent Dabbler Feb 19 '14 at 15:59
• Any restrictions in the usage of trailing whitespaces or zero-space unicode characters? – Thomas Weller Feb 20 '14 at 13:11
• @ThomasW. Yes: no (trailing) whitespace and/or zero-space unicode chars in the output (unless, for instance, a trailing newline is absolutely required by your language). – Decent Dabbler Feb 20 '14 at 14:09
• Point of clarification. Your example assigns the expressions to variables as do many of the solutions, but bullet point 2 says "Both expression may be assigned to variables". Does that mean we can just do E=m-expression*c-expression^2 without any other variable assignments? I can save a fair number of bytes if so. e.g. E=3*8631826306^2 ? – Jonathan Van Matre Feb 21 '14 at 23:28

# APL, 27

⎕UCS(2÷⍨⎕UCS'E=mc²')×(√2)*2


Ungolfed:

m←(⎕UCS 'E=mc²')÷2
c←√2
⎕UCS m×c*2


Start with the desired output string, convert it into an array of Unicode values, divide each number by 2, and assign the vector to m. Then assign √2 to c. Compute the formula and convert the resulting array back to a string. I can't tell whether the last operation is allowed by the rules, but it's akin to what most other solutions are doing, whether explicitly or implicitly.

• The ungolfed version does not seems to be less golfed than the golfed one to me. – Victor Stafusa Feb 20 '14 at 16:18
• @Victor it gives names to things (m, c) and it doesn't cram everything in a single line. Other than that, no, there's not much to ungolf here. – Tobia Feb 20 '14 at 16:41

# C, 6659 57 bytes

Ignore the compiler warnings. It should work OK as long as you compile it on a big little-endian machine. This code could probably be made a lot shorter on a 64-bit machine, but I'm unable to test:

main(){long long m=1130224008533LL,c=7,E=m*c*c;puts(&E);}


9 fewer characters thanks to @Adam Davis and @Michael :-)

• This even works on Ideone: ideone.com/SCqKFm – ComFreek Feb 19 '14 at 18:11
• So far, your solution meets the requirements most strictly. As far as I can tell it ticks all the boxes. – Decent Dabbler Feb 19 '14 at 18:38
• You can save some space by using 0x1072699BD55 in place of 1130224008533LL. – Adam Davis Feb 19 '14 at 22:14
• Why is the LL required? long long m=1130224008533LL and long long m=1130224008533 are supposed to set m to the exact same value, and on my system, that does indeed work. – hvd Feb 20 '14 at 6:26
• Also, why don't you assign the value directly in E ? long long m=1130224008533LL,c=7,E=m*c*c; – Michael M. Feb 20 '14 at 14:06

## C, 57535040 39 bytes

The rules are somewhat confusing, I do hope I read them correctly.
Works on 32bit platforms only.

m="E=mc^2",c=-1u/2;
main(){
puts(m*c*c);
}


cc equals 2^31-1, so c*c equals 1 modulo 2^32. Therefore m*c*c == m (mod 2^32).
The -1u/2 constant and puts are hvd's suggestions.

• Sorry, but the rules stated that the expressions may not evaluate to 1 (c=1 here). – Decent Dabbler Feb 19 '14 at 15:55
• Thanks @fireeyedboy, fixed. And shortened while I'm at it. – ugoren Feb 19 '14 at 15:59
• Is the intermediate variable s necessary? (Or is that a constant perhaps?) Would something like m="E=mc^2"/4; not be possible, for instance? (Sorry, not a C programmer.) – Decent Dabbler Feb 19 '14 at 16:11
• I get "error: initializer element is not computable at load time" with GCC 4.8. – hvd Feb 19 '14 at 18:30
• @fireeyedboy That wouldn't work, compilers are lenient with implicit conversions between pointers and integers, but not lenient enough to allow division with a pointer operand. – hvd Feb 19 '14 at 18:35

# Java: 261241207 206 characters

Thanks to @V-X for helping me with ideas for optimizing this.

Here it is, 206 characters:

import java.math.*;class N{public static void main(String[]y){BigInteger m=new BigInteger("12uy4kc5e",36),c=new BigInteger("5"),e=m.multiply(c).multiply(c);System.out.println(new String(e.toByteArray()));}}


Properly indented version:

import java.math.*;
class N {
public static void main(String[] y) {
BigInteger m = new BigInteger("12uy4kc5e",36),
c = new BigInteger("5"),
e = m.multiply(c).multiply(c); // e = m * c * c
System.out.println(new String(e.toByteArray()));
}
}


Older version, Java 8 only, with 261 characters:

import java.math.*;public class N{public static void main(String[]y){BigDecimal m=new BigDecimal("59324209849366147.77"),c=BigDecimal.TEN,e=m.multiply(c).multiply(c);System.out.println(new String(java.util.Base64.getDecoder().decode(e.toBigInteger().toByteArray())));}}


Ungolfed version:

import java.math.*;
import static java.util.Base64.*;

public class N {
public static void main(String[] y) {
BigDecimal m = new BigDecimal("59324209849366147.77"),
c = BigDecimal.TEN,
e = m.multiply(c).multiply(c); // e = m * c * c

System.out.println(new String(getDecoder().decode(e.toBigInteger().toByteArray())));
}
}

• Java ftw! I really enjoy all your Java answers. – Justin Feb 19 '14 at 18:57
• Could reduce 4 bytes if I do not use an intermediate variable for e. But I prefer to use the intermediate var anyway. – Victor Stafusa Feb 19 '14 at 19:03
• by use of m=new BigDecimal("1130224008533"),c=new BigDecimal(7), you can get rid of 4 more characters... – V-X Feb 20 '14 at 12:27
• I have tried this import java.math.*; public class N { public static void main(String a[]) { BigInteger m = new BigInteger("1130224008533"), c = new BigInteger("7"), e = m.multiply(c).multiply(c); System.out.println(new String(e.toByteArray())); } } (215 chars after removing irrelevant spaces) but it was printing in the reversed order (I tried that in javalunch.com) – V-X Feb 20 '14 at 14:36
• I have another idea: you can transform BigInteger m = new BigInteger("3045205223042"); into BigInteger m = new BigInteger("12uy4kc5e",36); which is by 1 character shorter:) – V-X Feb 20 '14 at 16:13

# Perl - 32 characters

$m="E=mc²";$c=1.1;print$m x$c**2


This is sort of cheating: in Perl's x operator, the right operand is converted to an integer, so both 1.1 and 1.21 (1.1**2), despite having a value larger than 1, behave exactly as 1.

• Haha, sneaky. ;-) – Decent Dabbler Feb 19 '14 at 19:18

## TI-89 basic (needs CAS) - 17

solve(√(e/m)=c,e)


Edited - it now works (according to my calculator):

e = c^2 * m and c >= 0

• Looks neat, but would you mind explaining how this arrives at one of the requested solutions? – Decent Dabbler Feb 19 '14 at 16:08
• CAS = Computer Algebra System. It will solve the given equation (sqrt(em)=c) for e to give the incorrect equation e=c^2/m. – Kyle Kanos Feb 19 '14 at 16:16
• @KyleKanos gotcha, thanks. TheDoctor: back to the drawing board, I guess. ;-) Close though. – Decent Dabbler Feb 19 '14 at 16:22
• @fireeyedboy - oops... – TheDoctor Feb 19 '14 at 18:28
• Not seeing how does it comply with the rules, there aren't 2 expressions, for m and for c – mniip Feb 20 '14 at 15:27

# Lua - 74

c=rawset(_G,"print",function()io.write"E=mc^2\n"end)and 2 m=2 print(m*c^2)


A little cheaty, but not prohibited by the rules:

• Exactly 2 expressions, one for c, another for m.
• No intermediate variables

# Smalltalk, 11058 56

Version 2 below does it, but I guess version1 is interesting enough to not cut it. Read for your amusement (it's about fun here - isn't it)

### Version 1:

code:

(Class name:#S subclassOf:String)compile:'*t^''E='',self,t';compile:'squared^self,''²'''.m:='m'as:S.c:='c'as:S


evaluate:

m*c squared


result: 'E=mc²'

I really have a hard time to understand what you expect :-(, so I'll give some more detail than usual. Maybe it is of interest to others, even if it does not golf.

First, you want the standard language syntax to be used, eg. in Smalltalk, I would write:

m := 100.
c := 299792500.
E := m * c squared.


to get "8987554305625000000".

So you want this to be done symbolically. In ST we need a class which redefines the above used operators into string concatenations (any class would do, but I'll inherit from String for convenience, so I get the ,-operator):

Class name:#MyString subclassOf:String

* thingy
^ self,thingy asString

squared
^ self,'²'

= thingy
^ self,'=',thingy


So now, I can write:

m := MyString fromString:'m'.  "/ or 'm' as:MyString for short
c := MyString fromString:'c'.
(m * c squared) print


and get 'mc²' as output.

However, and that's my problem: the assignment cannot be redefined, so saying:

E := (m * c squared).
E print


would print the same. In Smalltalk, "=" is a comparison operator (not assignment). "=" was already redefined in the above code. However, as I am calling for a comparison of E against the right side, I need to preset E to something non-nil first:

E := MyString fromString:'E'.


So let's write:

(E = m * c squared) print


gives us (the requested?): 'E=mc²'

As you wanted the result of m*c squared, I'll push the 'E' into the multiplication operator:

* thingy
^ 'E=',self,thingy


PS: the second comment made it obvious; all I have to change is the assignment in the above class to generate the 'E'.

### Version 2:

Obviously I was thinking way too complicated:

m:=36rEF7U6YC5.c:=7.E:=m*c squared.E digitBytes asString


also outputs: 'E=mc^2'

Edit: The large number 1130224008533 is obviously shorter if written in a higher base - hex for example; but it is much shorter in base 36, which in Smalltalk is written as 36rEF7U6YC5. This saves me another 2 characters.

PS: how to get to the magic numbers:
LargeInteger digitBytes:'E=mc^2' asByteArray
-> 55380976418117
55380976418117 primeFactors
-> Bag('4728970747(*1)' '7(*2)' '239(*1)')

• So where's the answer? You need to provide a program, and say how long it is. Then you can explain how and why. – ugoren Feb 19 '14 at 18:22
• Basically what I'm looking for is: have two expressions m and c, for which the result of m times c squared, when printed as a string, will yield E=mc². Have a look at squeamish ossifrage's solution in C, for an example. – Decent Dabbler Feb 19 '14 at 18:54
• that made it clear; I hope I got it right now. – blabla999 Feb 19 '14 at 19:18
• Sorry, I know next to nothing about Smalltalk to evaluate whether your first solution was too complicated, although I had a strong feeling it was. But your second solution seems to confirm that indeed it was. ;-) That looks a lot more like what I expected to find and what others have come up with as well. Nice one! – Decent Dabbler Feb 19 '14 at 19:39

# T-SQL - 86 bytes 75 bytes

I'm pretty proud of how competitive this one is for SQL the verbose blabbermouth.

Found myself wishing SQL had a 64-bit binary data type so I could possibly find a bigger square root than 3, but this comes out as a pretty lean one-liner anyway. And it prints the pretty Unicode version! (Which is good, because there were no squares to factor out of the number for E=mc^2)

And it smokes the Java entry. ;-)

Jus' kiddin' - @Victor and I are kindred spirits, more interested in playing our best game than winning the prize. Here's to we the crazy ones!

### New Hotness, 75 bytes

I was hoping for a larger improvement, but due to the vagaries of the query processor I have to explicitly declare @m as a BIGINT or SQL chooses the wrong bit size and the binary result is wrong. Still, better is better.

DECLARE @m BIGINT=33042591394;SELECT CAST(CAST(@m*3*3 AS BINARY(5))AS CHAR)


### Original Submission, 86 bytes

DECLARE @c INT=3,@m BIGINT=33042591394;SELECT CAST(CAST(@m*@c*@c AS BINARY(5))AS CHAR)


SQLFiddle Here.

Output:

E=mc²

• I like it! Nice. :-) – Decent Dabbler Feb 20 '14 at 18:04

## C# 180 176 171 170 165

using System;namespace N{class P{static void Main(){var m=0x1072699BD55;var c=7;Console.Write(System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(m*c*c)));}}}


Concept like the Smalltalk version, just for C#.
Output: E=mc^2
Use the longer version var m=0x391DAF9277DEA5D; for E=mc² (actually followed by two spaces)

Edits:
180 to 176: remove 4 spaces
176 to 171: inline usings
171 to 170: Use UTF8 encoding
170 to 165: WriteLine->Write, outline using System; (thanks for the comments)

• PauloHDSousa suggested that you add using System; at the beginning and change System.Console.WriteLine to Console.Write and System.BitConverter to BitConverter. His suggestion: using System;namespace N{class P{static void Main(){var m=1130224008533;var c=7;Console.Write(System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(m*c*c)));}}} I think that System.Text.Encoding could probably just become Text.Encoding (Seems logical). His suggestion saves 6 bytes, but if my idea works, you save 7+6=13 bytes – Justin Feb 20 '14 at 18:28
• Unfortunately System.Text.Encoding cannot be shortened as proposed. BTW: how to count bytes reliably? Somehow my count differs by 1. – Thomas Weller Feb 21 '14 at 2:09