查找小数表示形式的重复!


12

在2年前的这一挑战中,我们发现了一个单位分数()的周期1/n where n is a natural number

现在,您的任务是编写一个程序/函数来查找单位分数的重复项。

repetend是十进制扩展其重复无限,等的部分:

  • 的十进制表示形式1/60.16666...,则重复数6
  • 的十进制表示形式1/110.090909...,则重复数为09
  • 的十进制表示形式1/280.0357142857142857142857...,则重复数为571428

眼镜

  • 以任何合理的格式输入。
  • 用十进制,字符串或list输出重复。
  • 对于1/70.142857142857...),您必须输出142857而不是428571
  • 对于1/130.076923076923076923...),您必须输出076923而不是76923
  • 请不要蛮力。

测试用例

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

计分

这是。以字节为单位的最短解决方案获胜。

没有答案会被接受,因为目标不是找到能够产生最短解决方案的语言,而是找到每种语言中最短解决方案的语言。

排行榜



1
您如何确定13的重复是076923,而不是769230?
aidtsu退出是因为SE为EVIL,2016年

@aditsu因为1/130.076923076923...0.769230769230...
漏嫩

3
公开声明您几乎永远不会接受答案,因此将其定为目录。只是什么都不要说,永远不要接受答案。
丹尼斯

1
您可以添加堆栈片段以显示每种语言的最短解决方案。
aidtsu退出是因为SE为EVIL,2016年

Answers:


5

Java,150字节:

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;
    }
}

完整的程序:

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(;;)会比while(2<3)无限个字节少,但同时也是一个无限循环!(比while(1)@Maltysen还少的字节)
Marv

@Marv我怎么能忘记呢?谢谢!
Leaky Nun

把的声明a,并r在for循环。节省字节!
CalculatorFeline

1
@CatsAreFluffy这将使它们无法访问...
Leaky Nun

3

果酱,26

riL{_XW$%A*:X|X@-}g_X#>\f/

在线尝试

说明:

该程序会建立一系列的小数,这些小数涉及小数扩展的计算,直到找到以前看到的小数。然后,将所有股息从该股息开始,然后将其除以n,得到返还的数字。

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


2

果冻12 10 字节

%³×⁵
1ÇÐḶ:

通过跟踪股息节省了2个字节,这是我从@aditsu的CJam答案中得出的想法

在线尝试!

怎么运行的

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

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


1ÇÐḶ:    Main link. Argument: n

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.

1

GameMaker语言,152字节

根据肯尼的答案

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}

我只是在我的方法中发现了一个错误并将其修复,因此也许您也需要对其进行更新。
Leaky Nun

1

爪哇,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;}

类似于我的CJam解决方案。


1

Perl 6、37个字节

{(1.FatRat/$_).base-repeating[1]||~0}
~0 max (1.FatRat/*).base-repeating[1]

如果您不在乎它仅适用于适合64位整数的分母,则可以删除对的方法调用.FatRat

说明:

# 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]

测试:

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

my &code = ~0 max (1.FatRat/*).base-repeating[1];
# stupid highlighter */
# Perl has never had // or /* */ comments

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


0

PHP,169字节

$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));
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.