6
\$\begingroup\$

Description:

A non-integer representation of a number uses non-integer numbers as the bases of a positional numbering system.

e.g. Using the golden ratio (the irrational number 1 + √5/2 ≈ 1.61803399 symbolized by the Greek letter φ) as its base, the numbers 1 to 10 can be written:

Decimal Powers of φ             Base φ
1       φ0                      1    
2       φ1 + φ−2                10.01 
3       φ2 + φ−2                100.01  
4       φ2 + φ0 + φ−2           101.01  
5       φ3 + φ−1 + φ−4          1000.1001
6       φ3 + φ1 + φ−4           1010.0001
7       φ4 + φ−4                10000.0001
8       φ4 + φ0 + φ−4           10001.0001
9       φ4 + φ1 + φ−2 + φ−4     10010.0101
10      φ4 + φ2 + φ−2 + φ−4     10100.0101

Note that just as with any base-n system, numbers with a terminating representation have an alternative recurring representation. In base-10, this relies on the observation that 0.999...=1. In base-φ, the numeral 0.1010101... can be seen to be equal to 1 in several ways:

Conversion to nonstandard form: 1 = 0.11φ = 0.1011φ = 0.101011φ = ... = 0.10101010....φ

One representation in a non-integer base can be found using the greedy algorithm.

Challenge:

Given a number n, a non-integer real base b (1 < b < 10, since, as @xnor pointed out in comments, representation in current format would be nonsensical), and decimal places l, output a non-integer representation of n in base b to l decimal places.

Test Cases:

n=10, b=(1 + Sqrt[5])/2, l=5,     ->     10100.01010
n=10, b=E, l=5,                   ->     102.11201
n=10, b=Pi, l=5,                  ->     100.01022
n=10, b=1.2, l=14,                ->     1000000000001.00000000000001
n=10^4, b=Sqrt[2], l=12,          ->     100001000010010000010000100.000000000001

Note any valid representation is allowed. e.g.

n=450, b=E, l=6,     ->     222010.221120

or

n=450, b=E, l=6,     ->     1002020.211202

Prohibitions:

Calling Wolfram Alpha or any other external computational site is disallowed. Standard loopholes apply. This is Code Golf, so shortest code wins.

\$\endgroup\$
  • \$\begingroup\$ Can our output be two lists, one of digits and one of decimal places, rather than a string? \$\endgroup\$ – notjagan Jul 19 '17 at 19:05
  • \$\begingroup\$ @notjagan yes that's fine \$\endgroup\$ – martin Jul 19 '17 at 19:26
  • 1
    \$\begingroup\$ @martin non-integer inputs in what form? Can there be sin? mod? floor? Is b always given as a rational approximation of the input? This is missing a clear input spec at the moment... \$\endgroup\$ – Socratic Phoenix Jul 19 '17 at 19:43
  • 2
    \$\begingroup\$ @martin I believe he means whether we need to deal with an input of "sin(1+sqrt(5))", i.e. whether we need to evaluate those expressions. \$\endgroup\$ – Leaky Nun Jul 19 '17 at 19:45
  • 1
    \$\begingroup\$ @martin Can the digits be negative numbers? Or, what if they are above 9? And I take it the digits must be integers? \$\endgroup\$ – xnor Jul 19 '17 at 22:39
1
\$\begingroup\$

Mathematica, 85 bytes

(r=RealDigits[#,#2,9#3];k=r[[2]];s=ToString/@r[[1]];""<>s[[;;k]].""<>s[[k+1;;k+#3]])&

Mathematica, 86 bytes

(s=TakeDrop@##&@@RealDigits[#,#2,9#3];FromDigits@s[[1]].""<>ToString/@s[[2]][[;;#3]])&

input

[450,E,6]

\$\endgroup\$
5
\$\begingroup\$

Python 3, 151 134 121 120 bytes

import math
def f(n,b,l,r=''):
 for i in range(int(math.log(n,b)),-l-1,-1):r+="%i"%(n/b**i)+'.'*(0==i);n%=b**i
 return r

Try it online!

Returns a string of the non-integer base representation. This can probably be golfed more; stay tuned.


Special thanks to:

  • @LeakyNun for saving 13 bytes!
  • @Zacharý for saving 1 byte!
\$\endgroup\$
  • \$\begingroup\$ 121 bytes \$\endgroup\$ – Leaky Nun Jul 19 '17 at 19:50
  • \$\begingroup\$ @LeakyNun woah. What's going on in "%i"%(n/b**i)? I've not seen syntax like that before. \$\endgroup\$ – icosahedron Jul 19 '17 at 19:53
  • 1
    \$\begingroup\$ It is string formatting. "%i" is the string to be formatted, with parameter n/b**i \$\endgroup\$ – Leaky Nun Jul 19 '17 at 20:01
  • 1
    \$\begingroup\$ You can save a single byte by using import math then math.log. \$\endgroup\$ – Zacharý Jul 20 '17 at 15:54
  • \$\begingroup\$ @Zacharý good catch, thanks! \$\endgroup\$ – icosahedron Jul 20 '17 at 16:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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