7
\$\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\$
13
  • \$\begingroup\$ Can our output be two lists, one of digits and one of decimal places, rather than a string? \$\endgroup\$
    – notjagan
    Commented Jul 19, 2017 at 19:05
  • \$\begingroup\$ @notjagan yes that's fine \$\endgroup\$
    – martin
    Commented Jul 19, 2017 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\$ Commented Jul 19, 2017 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
    Commented Jul 19, 2017 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
    Commented Jul 19, 2017 at 22:39

4 Answers 4

6
\$\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\$
5
  • \$\begingroup\$ 121 bytes \$\endgroup\$
    – Leaky Nun
    Commented Jul 19, 2017 at 19:50
  • \$\begingroup\$ @LeakyNun woah. What's going on in "%i"%(n/b**i)? I've not seen syntax like that before. \$\endgroup\$
    – Chase
    Commented Jul 19, 2017 at 19:53
  • 1
    \$\begingroup\$ It is string formatting. "%i" is the string to be formatted, with parameter n/b**i \$\endgroup\$
    – Leaky Nun
    Commented Jul 19, 2017 at 20:01
  • 1
    \$\begingroup\$ You can save a single byte by using import math then math.log. \$\endgroup\$
    – Adalynn
    Commented Jul 20, 2017 at 15:54
  • \$\begingroup\$ @Zacharý good catch, thanks! \$\endgroup\$
    – Chase
    Commented Jul 20, 2017 at 16:01
2
\$\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\$
1
+200
\$\begingroup\$

APL (Dyalog Unicode), 47 bytes

{⌽⊃∘⍕¨⍺⍺(↑,'.',↓)⌊(⊣,⍨⍺×1|⊢)⍣(⍺⍺+z)⍨⍵÷⍺*z←⌊⍺⍟⍵}

Try it online!

A dop that takes n, b, l as (right arg), (left arg), ⍺⍺ (left operand) respectively. Does greedy conversion from the most significant digit.

How it works

{⌽⊃∘⍕¨⍺⍺(↑,'.',↓)⌊(⊣,⍨⍺×1|⊢)⍣(⍺⍺+z)⍨⍵÷⍺*z←⌊⍺⍟⍵}  ⍝ ⍵←n, ⍺←b, ⍺⍺←l

⍵÷⍺*z←⌊⍺⍟⍵  ⍝ Part 1
      ⌊⍺⍟⍵  ⍝ Floor of Log of n with base b
            ⍝ (number of digits above decimal point - 1)
    z←      ⍝ Assign to z
⍵÷⍺*        ⍝ Divide n by b^z, so that floor will give highest digit
            ⍝ Let's say the value is k

(⊣,⍨⍺×1|⊢)⍣(⍺⍺+z)⍨k  ⍝ Part 2
(        )⍣(⍺⍺+z)⍨k  ⍝ Repeat l+z times, using k as both the
                     ⍝ left arg (fixed) and right arg (moving)...
      1|⊢  ⍝ Fractional part of right arg
    ⍺×     ⍝ Multiply b
 ⊣,⍨       ⍝ Append left arg
⍝ The net effect is "prepend next significant digit, keeping fractional part"
⍝ Let's say the resulting array is v

⌽⊃∘⍕¨⍺⍺(↑,'.',↓)⌊v  ⍝ Part 3
                ⌊v  ⍝ Floor each number in v
     ⍺⍺(↑,'.',↓)    ⍝ Insert the decimal point after l digits
 ⊃∘⍕¨               ⍝ Convert each digit to a char
⌽                   ⍝ Reverse, so that the highest digit comes first
\$\endgroup\$
0
\$\begingroup\$

C (clang), 129 123 121 120 118 bytes (115 + -lm compiler flag)

f(n,b,l,i,y,z)double n,b,l,z;{for(i=log(n)/log(b);~i<l;n=fmod(n,z))z=pow(b,i),printf(".%d"+!!~i--,y=n/z);puts("");}

Try it online!

7 8 10 bytes shaved off thanks to ceilingcat!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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