Thread: fractions

  1. #16
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    my code

    i wrote out what i could but when i enter 45/56 it messes up and i am having trouble checking the next number.

    Code:
    #include <stdio.h>
    
    #define MAX 16
    
    int digit[MAX], x, y, z, a, j, k, q;
    double decimal;
    float n, d;
    void function( int h[], int p, int f);
    
    int main()
    {
        
        printf("Enter N/D: ");
        scanf("%f/%f", &n, &d);
    
        decimal = n / d;
        a = n;
        q = d;
       
        printf("%d / %d = %lf ", a, q, decimal);
        
        for ( x = 0; x < MAX; x++)
    	{
    	 digit[x] = (int)(decimal * 10);
    	 decimal = decimal * 10 - digit[x];
    	}
        
        for( z = 0; z< MAX; y++)    
            {
    
        for ( y = 1; y < MAX; y++)
    	{
    		if( digit[z] == digit[y])/*this is where it finds the first two matching number*/
    		/*
    		 k = z + 1; i decide to add one to z and one to k to see if the next number was repeating
             j = y + 1; so 45/56 would work. when i do this though the program doesnt work correctly
            
             if (digit[k] == digit[j])this is supposed to check the next number after it*/
              function( digit, y, z);/*if it is repeating then it continues on with the function*/
             
    	}
            }
        printf("\n");
        
    
        system("PAUSE");
    
        return 0;
    
    }
    
    
    
    void function( int h[], int p, int f)
    {
     int i;
        printf("or .");
            for (i = 0; i < f; i++)
             {
    		printf("%d", h[i]);
    	 }
    		printf("(");	
    	for ( i = f; i < p; i++)
     	 {
       	  printf("%d", h[i]);
    	 }
        printf(")\n");
     system("Pause");
     exit(1);
    }

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Check the maximum index values that you use for the array (consider changing loop limits to MAX-1).

    Dave

  3. #18
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Not having or read all of Knuth's books, I'm having trouble finding the exact quotation. But I believe Knuth defines in somewhere that .999999.. , and like numbers that terminate, *not* to be a repeating digits. This all seems a little up to interpretation, though.

    Here's some C code to test out my ideas written above.

    Code:
    #include <stdio.h>
    
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
    
        return gcd(b, a % b);
    }
    
    int repeating(int a, int b)
    {
    
        int g = gcd(a, b);
    
        a /= g;
        b /= g;
    
        printf("simplified (%d, %d)\n", a, b);
        while(b % 2 == 0) 
            b /= 2;
    
        while(b % 5 == 0)
            b /= 5;
    
    
        return b != 1;
    }
    
    int main(void)
    {
        int a, b;
    
        while(scanf("%d/%d", &a, &b) == 2) {
            if (repeating(a, b))
                printf("%f is repeating\n", (float)a/b);
            else
                printf("%f is not repeating\n", (float)a/b);
        }
    
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by okinrus
    Not having or read all of Knuth's books, I'm having trouble finding the exact quotation. But I believe Knuth defines in somewhere that .999999.. , and like numbers that terminate, *not* to be a repeating digits. This all seems a little up to interpretation, though.
    Well, all rational numbers are either terminated decimals (non repeating) or repeating decimals.

    If it is agreed that, for example, 2.5 = 2.50000... is a terminated decimal (writing the 000... is clearly surperfluous), then we probably should say that 2.49999... is also a terminated decimal (since it's the same number as 2.5).

    Now, your program identifies as non-repeating: rational numbers whose reduced fraction has a denominator consisting only of powers of 2 and powers of 5 (including the possibility of 2 to the zero power and/or 5 to the zero power.).

    If I concede the correctness of this, how does it help identify what the repeatend is? (The repeatend is the group of digits that are repeated. For example i know that 1/19 = .05263215.... is repeating, but so what?)

    I think that there may be some theorems about 1/p where p is a prime number (maximum length of the repeatend is p-1, but may be less). Probably lots of other theorems too for special cases.

    I think the discussion originated by considering how to identify the repeatend with a computer program. Your program identifies a given rational number as repeating, but does not help toward identifying the repeating digits, I think.

    Keep at it!

    Regards,

    Dave

  5. #20
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    If I concede the correctness of this, how does it help identify what the repeatend is? (The repeatend is the group of digits that are repeated. For example i know that 1/19 = .05263215.... is repeating, but so what?)
    I thought that was the original question. Knowing that the fraction is repeating, he could check (9a)/b, (99a)/b, (999a)/b, etc. in sequence to see if they too were repeating, For instance, if the number is 1/3 = .3333333 he would check 10 * (1/3) - 1/3 = 9 * (1/3) = 3. It would not be necessary to use any floating arith. but the possible checks would probably not have to exceed 10.

  6. #21
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    This is a more expanded version of what I gave above.

    Say we have x = .1234275275275... we could check 10x - x, 10^2x - x, 10^3x - x, 10^4x - x for a nonrepeating fraction. Because 10^4x - x is nonrepeating, we know that the repeat begins at decimal place 4. But instead of stoping at four, we may keep going, checking for the next nonrepeating number. That is, in this case we would check 10^5x - x, 10^6x - x, and 10^7x - x. Because 10^7x - x is nondivisible we stop there. We know the period is 7-4 and we may multiply the original number by 10^7. We perform the integer division, and we know that period least significant digits are the repeated digits.

  7. #22
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Something wrong with what I said above. Still, I think it can be done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM