find the repeted sequence in fractional part and enclose it in bracket

hi every one,

I am looking for a c program which will do the following task

1. accept 2 integers say a , b
2. perform the division
if a = 1 and b = 7
then a/b = 0.33333333333333333333333333333333
3. output shoud be 0.(3)

or if answer is 0.111221111122111112211
then it should display 0.(1112211)

or if it is 0.142857142857142857142857
then 0.(142857)

etc...


Comments

  • : hi every one,
    :
    : I am looking for a c program which will do the following task
    :
    : 1. accept 2 integers say a , b
    : 2. perform the division
    : if a = 1 and b = 7
    : then a/b = 0.33333333333333333333333333333333
    : 3. output shoud be 0.(3)
    :
    : or if answer is 0.111221111122111112211
    : then it should display 0.(1112211)
    :
    : or if it is 0.142857142857142857142857
    : then 0.(142857)
    :
    : etc...
    :
    :
    :

    The only safe way I can think of is to do the division yourself:
    [code]
    7/1.0.142857
    7
    ---
    30
    28
    --
    20
    14
    --
    60
    56
    --
    40
    35
    --
    50
    49
    --
    1 <---- Same number we started with, so repetition from here.
    [/code]

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • you could use sprintf() to store all the digits in a character buffer, then search that buf repeating sequences. Start out by checking for multiple occurrences of the first two decimal places. If those are found, then check for the first three. Keep adding another digit until the sequence is no longer found. strstr() should be your friend here.


  • : you could use sprintf() to store all the digits in a character buffer, then search that buf repeating sequences. Start out by checking for multiple occurrences of the first two decimal places. If those are found, then check for the first three. Keep adding another digit until the sequence is no longer found. strstr() should be your friend here.
    :
    :
    :
    That was my first thought too, but that's not safe.
    One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    And how about 1/6 = 0.1(6)?


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • : : you could use sprintf() to store all the digits in a character buffer, then search that buf repeating sequences. Start out by checking for multiple occurrences of the first two decimal places. If those are found, then check for the first three. Keep adding another digit until the sequence is no longer found. strstr() should be your friend here.
    : :
    : :
    : :
    : That was my first thought too, but that's not safe.
    : One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    : And how about 1/6 = 0.1(6)?
    :
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :
    :
    I like your idea, Eric, but it could be improved.
    If we get to a number we where at before.

    This is written from head with no knowledge of string funcs in C++...
    [code]
    int a = 1, b=6;
    int* array = new int[100];
    string s = "";

    for(int i = 0; i < 100; ++i){
    s += a / b;
    a = a % b;
    if(array[a] == 0)
    array[a] = i;
    else{ //found a match
    //don't want to write this right now: :-)
    s = s[0 to array[a]] + '(' + s[array[a] + 1 to end] + ')';
    break;
    }
    }
    [/code]

    I think the array could be minimized to the size of b...
    I also think I've done something wrong. The dot wont work with this...

    Happy coding wishes
    the one and only
    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • : :
    : That was my first thought too, but that's not safe.
    : One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    : And how about 1/6 = 0.1(6)?
    :
    :

    1/6 does not stop repeating. But I understand what you are saying. How far do you need to carry out the division before you give up and say that the sequence repeats forever? [b]Infinite[/b] is not an acceptable answer because the problem would never be solved.

  • : : :
    : : That was my first thought too, but that's not safe.
    : : One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    : : And how about 1/6 = 0.1(6)?
    : :
    : :
    :
    : 1/6 does not stop repeating. But I understand what you are saying. How far do you need to carry out the division before you give up and say that the sequence repeats forever? [b]Infinite[/b] is not an acceptable answer because the problem would never be solved.
    :
    :
    You're right about that. In that case, maybe you should combine both methods:
    String-search for a repeating pattern to suspect a repetition and try to prove it by hand-division.


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • : : :
    : : That was my first thought too, but that's not safe.
    : : One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    : : And how about 1/6 = 0.1(6)?
    : :
    : :
    :
    : 1/6 does not stop repeating. But I understand what you are saying. How far do you need to carry out the division before you give up and say that the sequence repeats forever? [b]Infinite[/b] is not an acceptable answer because the problem would never be solved.
    :
    :
    ....... the size is not a problem it is expected that it should work for size n... i mean if u declear size of the string 100 then it will consider only first 100 digits from fractional part... and so on
  • : : hi every one,
    : :
    : : I am looking for a c program which will do the following task
    : :
    : : 1. accept 2 integers say a , b
    : : 2. perform the division
    : : if a = 1 and b = 7
    : : then a/b = 0.33333333333333333333333333333333
    : : 3. output shoud be 0.(3)
    : :
    : : or if answer is 0.111221111122111112211
    : : then it should display 0.(1112211)
    : :
    : : or if it is 0.142857142857142857142857
    : : then 0.(142857)
    : :
    : : etc...
    : :
    : :
    : :
    :
    : The only safe way I can think of is to do the division yourself:
    : [code]
    : 7/1.0.142857
    : 7
    : ---
    : 30
    : 28
    : --
    : 20
    : 14
    : --
    : 60
    : 56
    : --
    : 40
    : 35
    : --
    : 50
    : 49
    : --
    : 1 <---- Same number we started with, so repetition from here.
    : [/code]
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :
    :
    ............... it will not work for many cases like
    a = 11 and b = 59
    then output is

    0.(1864406779661016949152542372881355932203389830508474576271)


    so at 2nd '1' we can not stop....

  • : : : :
    : : : That was my first thought too, but that's not safe.
    : : : One could have a number that seems to repeat, but stops repeating beyond the accuracy of the float or double.
    : : : And how about 1/6 = 0.1(6)?
    : : :
    : : :
    : :
    : : 1/6 does not stop repeating. But I understand what you are saying. How far do you need to carry out the division before you give up and say that the sequence repeats forever? [b]Infinite[/b] is not an acceptable answer because the problem would never be solved.
    : :
    : :
    : ....... the size is not a problem it is expected that it should work for size n... i mean if u declear size of the string 100 then it will consider only first 100 digits from fractional part... and so on
    :
    If you looked at my wrong code, and maybe my wrong statement, I think it could only repeat for max the denominator.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories