# Find the repetend of the decimal representation!

In this challenge 2 years ago, we found the period of a unit fraction (1/n where n is a natural number).

Now, your task is to write a program/function to find the repetend of a unit fraction.

The repetend is the part of the decimal expansion which repeats infinitely, like:

• The decimal representation of 1/6 is 0.16666..., then the repetend is 6.
• The decimal representation of 1/11 is 0.090909..., then the repetend is 09.
• The decimal representation of 1/28 is 0.0357142857142857142857..., then the repetend is 571428.

## Specs

• Input in any reasonable format.
• Output the repetend in decimal, string or list.
• For 1/7 (0.142857142857...), you must output 142857 instead of 428571.
• For 1/13 (0.076923076923076923...), you must output 076923 instead of 76923.

## Testcases

Input    Output
1        0
2        0
3        3
7        142857
13       076923
17       0588235294117647
28       571428
70       142857
98       102040816326530612244897959183673469387755
9899     000101020305081321345590463683200323264976260228305889483786241034447924032730578846348115971310233356904737852308313971108192746742095161127386604707546216789574704515607637135064147893726639054449944438832205273259925244974239822204263056874431760783917567431053641781998181634508536215779371653702394181230427315890493989291847661379937367410849580765733912516415799575714718658450348520052530558642287099707041115264168097787655318719062531568845337912920497019901

## Scoring

This is . Shortest solution in bytes win.

No answer would be accepted, because the goal is not to find the language capable of producing the shortest solution, but the shortest solution in each language.

• – Rɪᴋᴇʀ Apr 29 '16 at 17:45
• How do you decide that the repetend for 13 is 076923 and not 769230? – aditsu Apr 29 '16 at 18:27
• @aditsu Because 1/13 is 0.076923076923... not 0.769230769230... – Leaky Nun Apr 29 '16 at 18:38
• Openly stating that you will never accept an answer pretty much makes this a catalog. Just don't say anything and never accept an answer. – Dennis Apr 29 '16 at 18:56
• You could add a stack snippet to show the shortest solution for each language. – aditsu Apr 30 '16 at 7:13

# Java, 150 bytes:

String p(int n){int a=1,p;String r="";for(;n%10<1;n/=10);for(;n%2<1;n/=2)a*=5;for(;n%5<1;n/=5)a*=2;for(p=a%=n;;){p*=10;r+=p/n;if(a==(p%=n))return r;}}

String p(int n){
int a=1,p;
String r="";
for(;n%10<1;n/=10);
for(;n%2<1;n/=2)
a*=5;
for(;n%5<1;n/=5)
a*=2;
for(p=a%=n;;){
p*=10;
r+=p/n;
if(a==(p%=n))
return r;
}
}

### Ungolfed, full program:

import java.util.Scanner;

public class A036275 {
public static String period(int a,int n){
if(n%10==0) return period(a,n/10);
if(n%2==0) return period(a*5,n/2);
if(n%5==0) return period(a*2,n/5);
a %= n;
int pow = a;
String period = "";
while(true){
pow *= 10;
period += pow/n;
pow %= n;
if(pow == a){
return period;
}
}
}
public static String period(int n){
return period(1,n);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
System.out.println(period(n));
}
}
• for(;;) would be less bytes than while(2<3) though while also being an infinite loop! (and less bytes than while(1) also @Maltysen) – Marv Apr 30 '16 at 0:36
• @Marv How could I have forgot that? Thanks! – Leaky Nun Apr 30 '16 at 0:37
• Put the declarations for a and r in the for loops. Saves bytes! – CalculatorFeline Apr 30 '16 at 1:39
• @CatsAreFluffy It would make them inaccessible... – Leaky Nun Apr 30 '16 at 1:40

# CJam, 26

riL{_XW$%A*:X|X@-}g_X#>\f/ Try it online Explanation: The program builds a series of dividends involved in the calculation of the decimal expansion, until it finds a dividend it has seen before. Then it takes all the dividends starting with that one, and divides them by n to get the digits of the repetend. ri read the input and convert to integer (n) L push an empty array (will add the dividends to it) {…}g do … while _ copy the current array of dividends X push the latest dividend (initially 1 by default) W$     copy n from the bottom of the stack
%A*    calculate X mod n and multiply by 10
:X     store in X (this is the next dividend)
|      perform set union with the array of dividends
X@     push X and bring the old array to the top
-      set difference; it is empty iff the old array already contained X
this becomes the do-while loop condition
_X#      duplicate the array of dividends and find the position of X
>        take all dividends from that position
\f/      swap the array with n and divide all dividends by n

# Python 3.5, 797473 70 bytes

f=lambda n,t=1,q=[],*d:q[(*d,t).index(t):]or f(n,t%n*10,q+[t//n],*d,t)

Saved 3 bytes by keeping track of dividends, an idea I took from @aditsu's CJam answer.

Test it on repl.it.

# Jelly, 12 10 bytes

%³×⁵
1ÇÐḶ:

Saved 2 bytes by keeping track of dividends, an idea I took from @aditsu's CJam answer.

Try it online!

### How it works

%³×⁵     Helper link. Argument: d (dividend)

%³       Yield the remainder of the division of d by n.
×⁵     Multiply by 10.

1        Yield 1 (initial dividend).
ÇÐḶ     Apply the helper link until its results are no longer unique.
Yield the loop, i.e., everything from the first repeated result
up to and including the last unique one.
:    Divide each dividend by n.

n=argument0;a=1r=0while(n mod 2<1){a*=5n/=2}while(n mod 5<1){a*=2n/=5}a=a mod n;p=a;while(1){p*=10r=r*10+p/n;r=r mod $5f5e107;p=p mod n;if(a=p)return r} • I just found a bug in my approach and fixed it, so maybe you would need to update this as well. – Leaky Nun Apr 30 '16 at 0:57 # Java, 122 String f(int n){String r="";int x=1,a[]=new int[n*10];while(a[x]++<1)x=x%n*10;do{r+=x/n;x=x%n*10;}while(a[x]<2);return r;} Similar to my CJam solution. # Perl 6, 37 bytes {(1.FatRat/$_).base-repeating[1]||~0}
~0 max (1.FatRat/*).base-repeating[1]

If you don't care that it will only work with denominators that fit into a 64 bit integer you can remove the method call to .FatRat.

### Explanation:

# return ｢"0"｣ if ｢.base-repeating｣ would return ｢""｣
~0

# ｢&infix<max>｣ returns the numerically largest value
# or it's first value if they are numerically equal
max

(
# turn 1 into a FatRat so it will work
# with denominators of arbitrary size
1.FatRat

# divided by
/

# the input
*

# get the second value from calling the
# method ｢.base-repeating｣
).base-repeating[1]

### Test:

#! /usr/bin/env perl6
use v6.c;
use Test;

my &code = ~0 max (1.FatRat/*).base-repeating[1];
# stupid highlighter */

my @tests = (
1    => '0',
2    => '0',
3    => '3',
6    => '6',
7    => '142857',
11   => '09',
13   => '076923',
17   => '0588235294117647',
28   => '571428',
70   => '142857',
98   => '102040816326530612244897959183673469387755',
9899 => '000101020305081321345590463683200323264976260228305889483786241034447924032730578846348115971310233356904737852308313971108192746742095161127386604707546216789574704515607637135064147893726639054449944438832205273259925244974239822204263056874431760783917567431053641781998181634508536215779371653702394181230427315890493989291847661379937367410849580765733912516415799575714718658450348520052530558642287099707041115264168097787655318719062531568845337912920497019901',
);

plan +@tests;

for @tests -> $_ ( :key($input), :value($expected) ) { is code($input), $expected, "1/$input";
}
1..12
ok 1 - 1/1
ok 2 - 1/2
ok 3 - 1/3
ok 4 - 1/6
ok 5 - 1/7
ok 6 - 1/11
ok 7 - 1/13
ok 8 - 1/17
ok 9 - 1/28
ok 10 - 1/70
ok 11 - 1/98
ok 12 - 1/9899

# Pyth, 63 bytes

W!%QT=/QT;J*F+1-PQ,2 5K%*F+1-L7@,2 5PQJ=NKW1=+k/=*NTJIqK=%NJB;k

Try it online!

Direct translation of the answer in Java.

# PHP, 169 Bytes

$d=$argv[2];$a[]=$n=$argv[1];while($n%$d&&!$t){$n*=10;$r[]=$n/$d^0;$t=in_array($n%=$d,$a);$a[]=$n;}if($t)echo join(array_slice($r,array_search(end($a),$a),count(\$a)-1));