6
\$\begingroup\$

Fourward (Introduction)

I have an unhealthy obsession with the number 4. I love it so much, in fact, that seeing any other digit is frustrating to me. I therefour wish to create a 'Fourier Transformation': a way to represent any integer value with an expression that uses only the digit 4.

Fourmat (Challenge)

I wish to automate this process so I can use it everywhere. Your code must take any integer and output an expression that evaluates to that integer but which uses no digit other than '4'.

Four example, you might render the natural numbers as follows:

(4-4)
(4/4)
((4/4) + (4/4))
((4) - (4/4))
(4)
((4) + (4/4))
etc.

Any mathematical operation is allowed: you may use addition, multiplication, exponents, roots, logs, or even logical expressions such as AND or bitmasking--so long as no digit other than 4 appears in the output!

The parentheses aren't necessary; you may fourmat the output in whatever way you like, so long as it's readable. Also, because I'm not all that intelligent, the end result must be in base ten (or as I like to call it, base 4+4+(4/4)+(4/4).

Perfourmance (Criteria)

It is vitally important that the expressions each be as efficient as possible; I love the number 4, but I don't want to get sick of it. Submissions will thus be judged on the following, in order:

  • Efficiency of expressions (i.e. that the expression produced is as short as possible while still following the rules)
  • Efficiency of code (i.e. that your code is as short as possible)
  • Beauty of code (this is a tiebreaker; I won't be seeing the code itself so I don't mind non-four digits in there, but between two otherwise equal answers, I will prefer the code that uses the fewest non-four digits)
\$\endgroup\$
18
  • 1
    \$\begingroup\$ How length of expression is defined? \$\endgroup\$
    – l4m2
    Jan 19 at 17:04
  • 2
    \$\begingroup\$ How is "Efficiency of expressions" quantised? Is there a battery you will test our code with in order to score us; a range we may optimise over; or ...? \$\endgroup\$ Jan 19 at 17:55
  • 2
    \$\begingroup\$ @AosSidhe I have voted to (temporarily!) close the question since it is currently not precisely specified. If others agree, and the question goes into a closed state, please do not feel aggrieved, we just like to get things nice and clear before the answers come in (when tweaking rules becomes much more challenging!). The community will then be able to vote to reopen the question, as you address the concerns. For future reference we have a sandbox where one can take time to flesh challenges out and get community feedback. \$\endgroup\$ Jan 19 at 18:05
  • 1
    \$\begingroup\$ @AosSidhe I think you should explicitly define what the expressions we may output can be like (is 4.4 acceptable in an expression as per Neil's comment, etc). I would strongly advise against the open-ended and open-to-interpretation "any mathematical operation" route! \$\endgroup\$ Jan 19 at 18:10
  • 1
    \$\begingroup\$ To further expand on some things that may be unclear: How should logarithms be formatted log(a,b) log_a(b)? Is ln(a) allowed? is log(a) allowed and assumed to be 10? or 2? What order of operations are we using? Does bitwise & bind tighter or looser than multiplication or addition. You mentioned roots separately from exponents. How should those be formatted? Are intermediate decimal results allowed? I think this is a great challenge. Just clarify those things and it will be lots of fun. \$\endgroup\$
    – user197974
    Jan 19 at 18:14

1 Answer 1

2
\$\begingroup\$

JavaScript (Node.js), 26 bytes

f=n=>n?f(--n)+'+4/4':'4-4'

Try it online!

Quite simple and self explanatory. uses \$2n+2\$ 4's for any number \$n\$

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Fails for -1. Probably an easy fix. \$\endgroup\$ Jan 19 at 18:24

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