Golfscript - 26 bytes
{:i.)+.,{;10*i%.}%i>|,}:f;
Edit: updated to output 1
if the decimal terminates, rather than the length of the decimal representation.
A fairly efficient version. The value 67890 runs in approximately 10 seconds, and 99991 around 20 seconds. It's a bit slower than it was before (roughly half as fast), because the range that is iterated has been doubled, the first half of which is ignored.
Alternative, also 26 bytes
{:i.)+.n*{*i%.}%i>)^^,}:f;
This one works by iterating over the string "\n"*(2*i+1)
, where i
is the value passed to the function. The value passed to the block each time is the ordinal value of "\n"
, which is 10.
The )^^
is a bit of a work-around. When you uncons a character from a string, the result is the ordinal value of the character removed, as mentioned above. However, appending that value back on will append the string representation of that number, rather than the character - fairly nonsymmetric behavior, and in my opinion a design flaw. If you actually wanted to do that, stringifying first would only cost one byte.
An extra copy of the final value is already on the stack, so I remove the final value again )
, xor it with the string, and then xor it again, so that any characters which were added or removed by the first xor are restored. If int op string
were treated as a character, rather than its string representation, )^^
could be replaced by |
.
Note that while strings (which in Golfscript are stored as an array of ints) will display the value of each character mod 256, the values of each character may themselves be outside this range. When testing for uniqueness (via set operations) or containedness (via ?
), it is the actual value that is compared, rather than the display value.
A patch file for the current Golfscript interpreter:
61c61
< to_gs
---
> Gstring.new([self])
The above will only affect the behavior of string op int
(and vice versa), where op
is one of
+-|&^
. Everything else remains unaffected, including the behavior of Gint`
.
The following 24 byte solution would then become valid:
{:i.)+.n*{*i%.}%i>|,}:f;
And this also fixes a lot of other really ugly work-arounds.
Python - 48 bytes
f=lambda n:len(set(10**-~i%n for i in range(n)))
Not the most efficient solution, but reasonable for values less than 100000.
FWIW, the core element is identical to my solution for Generate cyclic numbers in decimal.
A more efficient version of the same code (70 bytes):
def f(n):
a=[];i=10%n
while i not in a:a+=i,;i=i*10%n
return len(a)
The value 99991 takes less than a second.
1.00000000000000000000000000000000000
\$\endgroup\$