1

Help me come up with an obfuscated way to multiply a number by 2, accurate to the second decimal.

Ideas:

  • use the Russian multiplication technique
  • trig / other mathematical identities
  • monte carlo methods
  • but of course bonus points for CS trickery

edit:

Just remembered that it's probably more appropriate to think of this in terms of significant figures, not accurate decimal places. So go for 4 matching leading digits.

Darth Egregious
  • 18,184
  • 3
  • 32
  • 54
  • 1
    I tagged as homework. Is this correct? – Jonathan M Oct 18 '11 at 19:06
  • Bonus points? Don't see a bounty anywhere. :) – bzlm Oct 18 '11 at 19:07
  • Jonathan M: No. Just curiosity on my part trying to make something simple seem complex. – Darth Egregious Oct 18 '11 at 19:08
  • 2
    @user973810, my point exactly. ;) Also, use the @ when replying; it even comes with autocompletion so you don't have to learn to spell. :) – bzlm Oct 18 '11 at 19:09
  • @JonathanM: No. Just curiosity on my part trying to make something simple seem complex. – Darth Egregious Oct 18 '11 at 19:20
  • 1
    @user973810: Since this is stackoverflow, I'm going to assume it involves code. What sort of assumptions have you made as to numerical representation, etc.? BCD vs IEEE 754 vs ASCII vs blah blah could help. I do know they're teaching kids a new weird form of multiplying large numbers that seems extremely labor intensive (I assume to minimize rote memorization). Is execution time a factor? I.e., do you have to stay in the realm of fadd and fabs? :-) – Art Taylor Oct 18 '11 at 19:57
  • 1
    @ArtTaylor assume that the input is a float in your language of preference. If the representation makes a difference feel free to exploit that. – Darth Egregious Oct 18 '11 at 20:16
  • 1
    In Soviet Russia numbers multiply *you*. – Dave Newton Oct 19 '11 at 01:06

4 Answers4

1

If the goal is obfuscation for the sake of it, there is nothing like some red herrings and useless object structure to distract whoever is reading the code from your true goals. For example, instead of using any number directly, you could pull it from a dictionary, or get it from the length of another object (say a list of size two), or even better, hide the number 2 in some string, and then regex it out with an awkward-to-read pattern.

Aurora
  • 4,384
  • 3
  • 34
  • 44
1

Since you want to make the simple complex, you could do some goofy things with complex numbers. Assuming you have any libraries available for complex arithmetic, you could, for example, leverage the most beautiful equation in mathematics: e^(pi*i) + 1 = 0. For instance in Java using Apache Commons Math (of course you would obfuscate the variable names):

Complex i = new Complex(0, 1);
double two = i.multiply(Math.PI).exp().getReal() + 3 +  i.multiply(Math.PI).exp().getImaginary()*5;

The real part is -1, so adding 3 gives us 2. The imaginary part is 0, so multiplying it by 5 and adding it is a red herring that doesn't do anything.*

As long as this is for fun, you can try other variants using other similar identifies. However, I don't recommend relying on this type of thing to truly obfuscate code within a real product. There are packages that obfuscate code for you, and automatically changing variable names to gibberish goes a long way to deterring humans (while still letting the code stay readable for the sanity of developers).

*In floating point arithmetic the imaginary part might not be exactly 0, but you said you were interested in accuracy to two decimal places.

Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
1

The following perl one-liner doubles the first command-line argument:

perl -e '$/=$\=shift;map$\+=$//(++$|+$|)**$_,(++$...$=);print'

You may say that using perl is cheating because everything is obfuscated in perl. You would not be entirely wrong.

Here's a slightly different approach in (unobfuscated) python:

import math
def double(n) :
    if n == 0 :
        return 0
    a = b = n
    for i in range(1,100) :
        a = 2 + 1.0/a
    a = a - 1
    for i in range(1,100) :
        b = a * b
        a = math.sqrt(a)
    return b
mhum
  • 2,928
  • 1
  • 16
  • 11
0

Since this is homework I don't want to just give you the answer but consider the number as it is represented in binary and what sort of binary operands are at your disposal that might help doing in doing multiplication.

Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
  • It's not homework and I don't think a bit shift is obfuscation. I'm thinking of something along these lines, but for a specific number: http://stackoverflow.com/questions/260511/russian-peasant-multiplication – Darth Egregious Oct 18 '11 at 19:13
  • 1
    @user973810: Ah ok, I wasn't sure how basic it was. When I saw homework I assumed it would be something like bit shifting. – Ian Dallas Oct 18 '11 at 19:15