7
\$\begingroup\$

Good Evening Golfers,

Your challenge, this evening, is logarithmic, and (as Abed would put it) "Meta"

Your goal is to write a program that takes an input from stdin or the command line, and returns the logarithm of that number, in the base of the length of your program in bytes.

So, if your program is 10 bytes, it must return the Log10 of the input number.

Rules:

  • Program must be longer then 1 character
  • Program must be able to accept floating point input in a form of your choice, or scientific format in a form of your choice with a minimum accuracy of 6 digits
  • Program must output to a minimum of 6 digits of accuracy in decimal or scientific format
  • Program must be entirely self contained, and must not access any files or network resources.

Your score in the natural logarithm of the amount of bytes in your source code.

Good luck.

\$\endgroup\$

15 Answers 15

16
\$\begingroup\$

APL 1.0986122886681098

3⍟⎕

⎕ Requests numerical screen input. Examples of use:

      3⍟⎕
      10
2.0959032742893844

      3⍟⎕
      1E1
2.0959032742893844

      3⍟⎕
      ○1
1.041978045992186

In the last example ○1 represents pi.

\$\endgroup\$
  • \$\begingroup\$ I count 7 bytes in your program so the score should be 1.9459101490553132. \$\endgroup\$ – Bakuriu Apr 20 '13 at 13:27
  • 4
    \$\begingroup\$ @Bakuriu. I am sorry if you are counting unicode bytes. I have explained this situation so many times now. APL+WIN uses a single byte character set but the only way I can get the characters to render properly on this site I have to convert the characters to UTF-8. The byte count for my solution is a genuine 3! \$\endgroup\$ – Graham Apr 20 '13 at 16:19
  • 1
    \$\begingroup\$ Then you should declare the encoding you use to let other people check. Probably the OP should have decided a canonical encoding to avoid ambiguous results, and if I were him I'd definitely choose utf-8. \$\endgroup\$ – Bakuriu Apr 21 '13 at 6:52
7
\$\begingroup\$

Mathematica: 2.079.. (length in bytes = 8)

8~Log~#&

Usage

8~Log~# &@8
(*
 1
*)
\$\endgroup\$
5
\$\begingroup\$

bc (with -l) - log(15) = 2.70805

l(read())/l(15)
\$\endgroup\$
5
\$\begingroup\$

BrainF***, 276000

++++++++++++++++++++++++[->++<].--.++......

... followed by just under e276200 of any byte except [, ], . or ,.

(When you present the score like that, it doesn't even seem so bad!)

\$\endgroup\$
4
\$\begingroup\$

Ruby: 2.995732273553991 (Math.log 20)

p Math.log$_.to_f,20

Sample run:

bash-4.2$ ruby -ne 'p Math.log$_.to_f,20' <<< 3.14
0.3819509540359265
\$\endgroup\$
4
\$\begingroup\$

J, 2.4849066497880003102297094798389 (12 chars)

12^.".1!:1[1

Usage:

   12^.".1!:1[1
12
1
\$\endgroup\$
4
\$\begingroup\$

Q (log(16) = 2.772589)

16 xlog"F"$(0:)0

Example

q)16 xlog"F"$(0:)0
16
1f

q)16 xlog"F"$(0:)0
1.6e1
1f

q)16 xlog"F"$(0:)0
13.912231123
0.9495705
\$\endgroup\$
3
\$\begingroup\$

Perl: 2.77258872223978 (log 16)

say log()/log 16

Sample run:

bash-4.2$ perl -nE 'say log()/log 16' <<< 3.14
0.412691139779226
\$\endgroup\$
  • \$\begingroup\$ $_=log()/log 15 with option -pe instead of -nE. \$\endgroup\$ – Heiko Oberdiek May 5 '14 at 18:33
2
\$\begingroup\$

Python (log 38) score: 3.6375861597263857 <- how embarrassing

>>> import math;print math.log(input(),38)
38**8
8.0
\$\endgroup\$
2
\$\begingroup\$

Tcl, 3.4011973816621555 (log 30)

puts [expr log($argv)/log(30)]
\$\endgroup\$
2
\$\begingroup\$

Lua 3.5835189384561 (36 38 42 chars)

L=math.log;print(L(io.read())/L(36))

Sample run:

C:...>echo 42 | lua -e "L=math.log;print(L(io.read'*n')/L(36))"  
1.0430165662508
C:...>echo 38 | lua -e "L=math.log;print(L(io.read'*n')/L(36))"  
1.0150877453695
C:...>echo 36 | lua -e "L=math.log;print(L(io.read'*n')/L(36))"  
1
C:...>
\$\endgroup\$
  • \$\begingroup\$ Do you really need to force the input into a number? For numeric input works anyway and for non-numeric input crashes anyway. (Beside that, handling input error was not required in the question.) You can spare 2 characters by using () instead of '*n': pastebin.com/461tUsse \$\endgroup\$ – manatwork Apr 18 '13 at 7:02
  • 1
    \$\begingroup\$ Hmm, apparently the '*n' isn't needed. Lua generally avoids automatic type coercions, but there are a few cases where strings do get converted to numbers. The team keeps threatening to remove them and a lot of Lua users just assume type coercions don't happen as a safety measure. So, this brings us down to 36 chars, as soon as I make the edit. \$\endgroup\$ – RBerteig Apr 18 '13 at 21:07
2
\$\begingroup\$

Smalltalk (Pharo 2.0) (41 ln) 3.713572066704308

Pharo -headless Pharo-2.0.image eval "FileStream stdin nextLine asNumber log:41"

No syntactic sugar is Smalltalk's pride, just regular sentence ;)

\$\endgroup\$
2
\$\begingroup\$

Scala - ln(46) = 3.82864...

print(math.log(args(0).toDouble)/math.log(46))

scala log.scala 12.34 -> 0.6563283834264416

\$\endgroup\$
1
\$\begingroup\$

CJam - ln(5) = 1.60943791243

rd5mL
\$\endgroup\$
1
\$\begingroup\$

JavaScript 69 bytes, score=4.23410650459726

function f(n){return Math.log(n)/Math.log(69);}

I counted bytes following this discussion: https://stackoverflow.com/questions/2219526/how-many-bytes-in-a-javascript-string

function byteCount(s) {          
        return encodeURI(s).split(/%..|./).length - 1;
    }

The number of characters is only 37. Any advice on counting bytes in code golf?

\$\endgroup\$
  • \$\begingroup\$ If you put your source code in a file, the file size will be the number of bytes \$\endgroup\$ – aditsu May 5 '14 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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