Bukankah lebih rapi jika fungsi pemrograman bisa terbalik, sama seperti fungsi matematika yang mereka implementasikan?
Tulis fungsi (atau program) yang mengambil satu input x
dalam bentuk apa pun, yang menghasilkan ln(x)
.
Ketika byte dari program disusun ulang / dibalik sehingga byte pertama sekarang adalah byte terakhir, ia harus mengambil satu input x
dalam bentuk dan output apa saja e^x
sebagai gantinya.
- Jawaban Anda harus memiliki setidaknya 3 angka penting yang benar.
- Perkiraan baik-baik saja, selama mereka memiliki setidaknya 3 angka signifikan yang benar.
- Kode Anda harus dalam bahasa pemrograman yang sama baik maju dan mundur.
Katakanlah program ini mengimplementasikan ln(x)
:
abc你好
Maka program ini harus mengimplementasikan e^x
:
\xBD\xA5\xE5\xA0\xBD\xE4cba
Bintang emas jika Anda menggunakan bahasa tanpa dukungan mengambang.
Ini adalah bentuk kode-golf yang aneh, sehingga program terpendek menang.
code-golf
arithmetic
source-layout
Filip Haglund
sumber
sumber
?
, yang menggunakan metode garis potong.Jawaban:
Haskell, 11 byte
dan dalam urutan terbalik:
Ini berfungsi tanpa trik "komentar". Alih-alih, setiap versi menentukan fungsi (
pxe
/gol
) tambahan yang tidak digunakan .sumber
gol=f
.APL, 3 bytes
This is a function train. Monadic
*
returnse^x
, monadic⍟
returnsln(x)
.⊣
is a dyadic function that returns its left argument. Thus,*⊣⍟
is equivalent to just*
, and the reverse⍟⊣*
is equivalent to just⍟
.sumber
Jelly,
54 bytesYay, my first Jelly answer. :) Input is via command-line argument.
Jelly has its own code page so each character is one byte.
Try it online!
Reversed:
Try it online!
Explanation
The
Æ
on its own is an unrecognised token, so it acts the same as a linefeed. That means in either case the main link is onlyÆl
orÆe
which is the 2-character built-in forexp()
orln()
and is by default performed on the first command-line argument.sumber
Javascript, 18 bytes
sumber
Math.ln||pxe.htaM
will probably also work.Seriously, 5 bytes
Input, ln, output, then exp on an empty stack (does nothing), and input (does nothing since input is exhausted). Try it online!
Reversed:
Try it online!
sumber
Julia, 7 bytes
This is an anonymous function. Assign it to a variable to call it. Evaluates to builtins
log
orexp
plus a comment.sumber
Mathematica, 19 bytes
Reversed:
This was interesting to golf! Mathematica has no line comments / implicit string endings, so I couldn't take the simple route. Instead, I used the fact that
0 + x == x
,0 x == 0
, and that1 x == x
, no matter whatx
is! Testing:sumber
Python2, 73 bytes
io: stdin/stdout
inverse:
sumber
__import__("math").
instead ofCJam, 11 bytes
Test it here.
Reversed:
Test it here.
Basically the same comment-trick as the OP's Python answer.
e#
starts a comment.rd
reads the input andml
orme
computes the logarithm or exponential.sumber
Brachylog, 3 bytes
Try it online!
Initially, I had hoped to use
~*
, but although*~
computese^x
and successfully ignores the trailing tilde,~*
fails for all integer inputs and hits a float overflow on most non-integer inputs.Forwards:
Backwards:
This uses the identity predicate because, although trailing tildes are tolerated, leading subscripts are not. (If they were, the Brachylog answer would be
*₁
alone, which is just the normal builtin for natural log.)sumber
Vitsy, 5 bytes
This is a program that exits on an error.
This program exits on an error with ln(input) on the stack.
Try it online! (note that I have put
N
to have visible output)Then it's inverse:
This program exits on an error with e^(input) on the stack.
Try it online!
sumber
Fuzzy Octo Guacamole, 7 bytes
non-competing, FOG is newer than the challenge
This is the equivalent of a function in FOG. It assumes the input is on the stack. This can be assigned to a function by the code
"EZO@pZE""f"o
, wheref
is any single-char name you want to assign. Then use it like any other command. Example:"EZO@pZE"'f'o^f
.Explanation:
Reversed:
sumber
Matl, 5 bytes
Yl
: logZe
: exp%
: commentsumber
Pyth, 12 bytes
Finds
ln(input())
Finds
e^input()
Spaces stop implicit printing of strings, each version calculates it then creates a string with the remaining characters.
ln(x)
mode heree^x
mode heresumber
𝔼𝕊𝕄𝕚𝕟, 8 chars / 10 bytes
Try it here (Firefox only).Try reverse here (Firefox only).
Just 2 builtins separated by a comment.
sumber
Jolf, 9 bytes
Program 1:
exp
of inputProgram 2:
ln
of inputBonus points for being a case-insensitive palindrome? Try it here!
sumber
J, 8 bytes
The natural logarithm is
^.
, and exponential^
. The problem is,.
can only modify a valid verb, otherwise, a spelling error will occur. Thus, we can't use the left argument trick in the APL answer, becuase^.[^
would cause an error when reversed, as^[.^
creates an invalid verb. So, we must use comments; butNB.
is so long :( Fortunately, they both end with.
, so&ldots; there's that.Logarithm:
Exponential:
You can enter them for yourself online!
sumber
Java 8,
19818230 bytesTry it online.
and reversed:
Try it online.
Uses the comment trick (
//
) with built-ins forMath.log
andMath.exp
.sumber
Runic Enchantments, 9 bytes
Try it online!
An ungodly uninteresting program.
@
insures termination of the implied entry point at the left, everything after is unexecuted. I tried really hard to re-use the'
orA
instructions, but to no avail, even at larger program sizes. The required explicit entry point for multi-line programs essentially precludes it.sumber