[ create a new paste ] login | about

Link: http://codepad.org/D99WD6ih    [ raw code | output | fork ]

C++, pasted on Nov 27:
#include <iostream>
using namespace std;
void foo(int n)
{
    int x = 1;
    int * initmod = new int[n];
    cout << "1 / " << n << " = 0.";
    int i = 0;
    while (true)
    {
        x = x * 10;
        cout << x / n;
        x = x % n;
        if (x % n == 0) break;
        for (int j = 0; j < i; j++)
            if (initmod[j] == x % n) goto exitloop;
        initmod[i++] = x % n;
    }
    cout << endl;
    return;
exitloop:
    cout << "..." << endl;
}

int main()
{
    for (int i = 2; i <= 50; i++)
    {
        foo(i);
    }
}


Output:
1 / 2 = 0.5
1 / 3 = 0.33...
1 / 4 = 0.25
1 / 5 = 0.2
1 / 6 = 0.16...
1 / 7 = 0.1428571...
1 / 8 = 0.125
1 / 9 = 0.11...
1 / 10 = 0.1
1 / 11 = 0.090...
1 / 12 = 0.083...
1 / 13 = 0.0769230...
1 / 14 = 0.0714285...
1 / 15 = 0.06...
1 / 16 = 0.0625
1 / 17 = 0.05882352941176470...
1 / 18 = 0.05...
1 / 19 = 0.0526315789473684210...
1 / 20 = 0.05
1 / 21 = 0.0476190...
1 / 22 = 0.045...
1 / 23 = 0.04347826086956521739130...
1 / 24 = 0.0416...
1 / 25 = 0.04
1 / 26 = 0.0384615...
1 / 27 = 0.0370...
1 / 28 = 0.03571428...
1 / 29 = 0.03448275862068965517241379310...
1 / 30 = 0.03...
1 / 31 = 0.0322580645161290...
1 / 32 = 0.03125
1 / 33 = 0.030...
1 / 34 = 0.02941176470588235...
1 / 35 = 0.0285714...
1 / 36 = 0.027...
1 / 37 = 0.0270...
1 / 38 = 0.0263157894736842105...
1 / 39 = 0.0256410...
1 / 40 = 0.025
1 / 41 = 0.024390...
1 / 42 = 0.0238095...
1 / 43 = 0.0232558139534883720930...
1 / 44 = 0.0227...
1 / 45 = 0.02...
1 / 46 = 0.02173913043478260869565...
1 / 47 = 0.02127659574468085106382978723404255319148936170...
1 / 48 = 0.02083...
1 / 49 = 0.0204081632653061224489795918367346938775510...
1 / 50 = 0.02


Create a new paste based on this one


Comments: