fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define N 100
  4.  
  5. int div(int x)
  6. {
  7. int r = 1; // remainder 余り
  8. int q; // quotient 商
  9. int m[N]; // メモリー
  10. int i, j;
  11.  
  12. m[0] = r;
  13. printf("0.");
  14. for (i = 1; i < N; i++) {
  15. r *= 10;
  16. q = r / x;
  17. printf("%d", q);
  18. r -= x * q;
  19. if (r <= 0) return 0; // 割り切れる
  20. for (j = 0; j < i; j++) {
  21. if (m[j] == r) return i; // 循環小数
  22. }
  23. m[i] = r;
  24. }
  25. return -1; // 配列が足りない
  26. }
  27.  
  28. int main()
  29. {
  30. int x;
  31. int n; // 循環節
  32. int i = 0; // 一番長い循環節
  33.  
  34. for (x = 2; x <= N; x++) {
  35. n = div(x);
  36. printf("\nx=%d n=%d\n", x, n);
  37. if (i < n) i = n;
  38. }
  39. printf("i=%d\n", i);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
0.5
x=2 n=0
0.3
x=3 n=1
0.25
x=4 n=0
0.2
x=5 n=0
0.16
x=6 n=2
0.142857
x=7 n=6
0.125
x=8 n=0
0.1
x=9 n=1
0.1
x=10 n=0
0.09
x=11 n=2
0.083
x=12 n=3
0.076923
x=13 n=6
0.0714285
x=14 n=7
0.06
x=15 n=2
0.0625
x=16 n=0
0.0588235294117647
x=17 n=16
0.05
x=18 n=2
0.052631578947368421
x=19 n=18
0.05
x=20 n=0
0.047619
x=21 n=6
0.045
x=22 n=3
0.0434782608695652173913
x=23 n=22
0.0416
x=24 n=4
0.04
x=25 n=0
0.0384615
x=26 n=7
0.037
x=27 n=3
0.03571428
x=28 n=8
0.0344827586206896551724137931
x=29 n=28
0.03
x=30 n=2
0.032258064516129
x=31 n=15
0.03125
x=32 n=0
0.03
x=33 n=2
0.02941176470588235
x=34 n=17
0.0285714
x=35 n=7
0.027
x=36 n=3
0.027
x=37 n=3
0.0263157894736842105
x=38 n=19
0.025641
x=39 n=6
0.025
x=40 n=0
0.02439
x=41 n=5
0.0238095
x=42 n=7
0.023255813953488372093
x=43 n=21
0.0227
x=44 n=4
0.02
x=45 n=2
0.02173913043478260869565
x=46 n=23
0.0212765957446808510638297872340425531914893617
x=47 n=46
0.02083
x=48 n=5
0.020408163265306122448979591836734693877551
x=49 n=42
0.02
x=50 n=0
0.0196078431372549
x=51 n=16
0.01923076
x=52 n=8
0.0188679245283
x=53 n=13
0.0185
x=54 n=4
0.018
x=55 n=3
0.017857142
x=56 n=9
0.017543859649122807
x=57 n=18
0.01724137931034482758620689655
x=58 n=29
0.0169491525423728813559322033898305084745762711864406779661
x=59 n=58
0.016
x=60 n=3
0.016393442622950819672131147540983606557377049180327868852459
x=61 n=60
0.0161290322580645
x=62 n=16
0.015873
x=63 n=6
0.015625
x=64 n=0
0.0153846
x=65 n=7
0.015
x=66 n=3
0.014925373134328358208955223880597
x=67 n=33
0.014705882352941176
x=68 n=18
0.0144927536231884057971
x=69 n=22
0.0142857
x=70 n=7
0.01408450704225352112676056338028169
x=71 n=35
0.0138
x=72 n=4
0.01369863
x=73 n=8
0.0135
x=74 n=4
0.013
x=75 n=3
0.01315789473684210526
x=76 n=20
0.012987
x=77 n=6
0.0128205
x=78 n=7
0.0126582278481
x=79 n=13
0.0125
x=80 n=0
0.012345679
x=81 n=9
0.012195
x=82 n=6
0.01204819277108433734939759036144578313253
x=83 n=41
0.01190476
x=84 n=8
0.01176470588235294
x=85 n=17
0.0116279069767441860465
x=86 n=22
0.0114942528735632183908045977
x=87 n=28
0.01136
x=88 n=5
0.01123595505617977528089887640449438202247191
x=89 n=44
0.01
x=90 n=2
0.010989
x=91 n=6
0.010869565217391304347826
x=92 n=24
0.010752688172043
x=93 n=15
0.01063829787234042553191489361702127659574468085
x=94 n=47
0.0105263157894736842
x=95 n=19
0.010416
x=96 n=6
0.010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567
x=97 n=96
0.0102040816326530612244897959183673469387755
x=98 n=43
0.01
x=99 n=2
0.01
x=100 n=0
i=96