Quantcast
Channel: All about basic binary - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 23

Answer by lirtosiast for All about basic binary

$
0
0

TI-BASIC, 31 29

For(B,2,AnsIf 2>round(Bmax(fPart(Ans/B^randIntNoRep(1,32Disp BEnd

This is probably optimal for TI-BASIC.

Explanation:

randIntNoRep(1,32) returns a random permutation of the numbers from 1 to 32 (All we need is those numbers in some order; TI-BASIC doesn't have anything like APL's iota command). 32 elements is enough because the smallest possible base is 2 and the largest number is 2^32-1. B^randIntNoRep(1,31) raises that list to the Bth power, which results in the list containing all of B^1,B^2,...,B^32 (in some order).

Then the input (in the Answer variable, which is input to in the form [number]:[program name]) is divided by that number. If your input is 42 and the base is 2, the result will be the list 21,10.5,5.25,...,42/32,42/64,[lots of numbers less than 1/2], again in some order.

Taking the fractional part and multiplying the number by your base gives the digit at that position in the base-b representation. If all digits are less than 2, then the greatest digit will be less than 2.

As Ypnypn stated, a closing parenthesis on the For statement speeds this up due to a parser bug.

31->31: Saved a byte but fixed rounding errors which added the byte again.

31->29: Saved two bytes by using RandIntNoRep() instead of cumSum(binomcdf()).


Viewing all articles
Browse latest Browse all 23

Trending Articles