Announcement

Collapse
No announcement yet.

Reduce fractions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reduce fractions

    How can I reduce the fractions, if possible, make the fractions
    mixed numbers. EX. 10/8=5/4 = 1 1/4.

    I have no clue.

    -------------
    Kro121
    Kro121

  • #2
    as you already have your numerator and denominator, you can calculate the whole number directly, and then use some of the bbs code to create the fractional part.

    a search of the bbs for "fraction" brings up some code that should help with this problem... try some of the following:





    ------------------
    lance
    powerbasic support
    mailto:[email protected][email protected]</a>
    Lance
    mailto:[email protected]

    Comment


    • #3
      how bout this
      input "Enter a decimal",num$
      for k%=1 to len(num$)
      if mid$(num$,k%,1)="." then
      if k%<>1 then whole&=val(mid$(num$,1,k%))
      numerator&=val(mid$(num$,k%+1))
      denom&=10^(len(num$)-k%)
      end if
      next k%

      for m&=numerator&/2 to numerator&-1
      if numerator&/m&=int(numerator&/m&) and denom&/m&=int (denom&/m&) then divisor&=m&
      next m&
      if divisor&<>0 then numerator&=numerator&/divisor& :
      denim&=denom&/m&

      print whole&;ltrim$(str$(numerator&));"/";ltrim$(str$(denim&))



      ------------------
      Client Writeup for the CPA

      buffs.proboards2.com

      Links Page

      Comment


      • #4
        I think i had a mental breakdown there LOL
        the routine to check for a divisor should go
        from 2 to numerator/2 instead

        you could also check for a "/" entered then use the smaller
        of numerator or denuminator for finding a divisor.

        Actually you could do it from 1 to numerator&-1
        but there is no need to do all of those calcuilations.


        ------------------
        Client Writeup for the CPA

        buffs.proboards2.com

        Links Page

        Comment


        • #5
          That's still not right sorry.
          This is better

          for m&=numerator to 2 step -1
          if numerator&/m&=int(numerator&/m&) and denom&/m&=int
          (denom&/m&) then
          divisor&=m&
          exit for
          end if
          next m&
          **and there was a mistake on a later line too
          if divisor&<>0 then numerator&=numerator&/divisor& :
          denim&=denom&/m&
          print whole&;ltrim$(str$(numerator&));"/";ltrim$(str$(denim&))


          should be
          if divisor&<>0 then numerator&=numerator&/divisor& :
          denom&=denom&/divisor&
          print whole&;ltrim$(str$(numerator&));"/";ltrim$(str$(denom&))




          ------------------
          Client Writeup for the CPA

          buffs.proboards2.com

          Links Page

          Comment


          • #6
            Oops. I posted before really reading the original question. I do have an algorithm for creating fractions from decimal, and reducing them, based on an algorithm by Euclid. It's very efficient. But It doesn't handle creating mixed fractions. But if someone's interested, I'll post it to the source code forum



            [This message has been edited by Michael Torrie (edited May 13, 2000).]

            Comment


            • #7
              There is a program I wrote that does what you want --
              It converts any decimal number to a rational
              equivalent, even for "irrational" numbers. It
              depends upon how many places of accuracy you are
              willing to settle for. It is called RATIONAL, and
              it has been posted at the ABC (All BASIC Code) web
              site. I wrote the original version on a TI SR-52
              calculator many years ago, and adapt it to every
              new computer language that I learn. It can be
              handy to have.

              Rational numbers have the form of P/Q, so that you
              get both the P and the Q as integer values. If you
              want to convert the result to a integer and fraction
              form. you would just complete the division, take the
              INT part for your integer, then use RATIONAL to
              convert the decimal portion into another P/Q
              expression for the fraction.

              ------------------

              Comment


              • #8
                Lance, it seems like two of the links you mentioned return an
                "HTTP 404 - File not found" error. However I searched in the
                archived forums, and found those messages there.

                You guys may be interested in the code listed at VbToTheMax.com
                ( http://www.vb2themax.com/Item.asp?Pa...odeBank&ID=344 )
                designed just for that purpose. The algorithm is based on the
                concept of Continued Fractions. And it can return results for
                use in the form "a/b" or "a b/c".


                ------------------
                Daniel Corbier
                UCalc Fast Math Parser
                http://www.ucalc.com

                [This message has been edited by Daniel Corbier (edited April 09, 2001).]
                Daniel Corbier
                uCalc Fast Math Parser
                uCalc Language Builder
                sigpic

                Comment


                • #9
                  Daniel, I posted that message in May 2000, so normal archiving has shifted their location.

                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  Lance
                  mailto:[email protected]

                  Comment


                  • #10
                    I missed it on the first try. This one should reduce


                    Code:
                    num = 10
                    denom = 8
                    whole = FIX(num/denom)
                    num = num MOD denom
                    
                    DO 
                       FOR x = 2 TO num
                         IF num MOD x = 0 AND denom MOD x = 0 THEN
                            num = num / x : denom = denom / x
                            ITERATE DO
                         END IF
                      NEXT X
                      EXIT DO
                    LOOP
                    
                    
                    print  STR$(whole) + " " + STR$(num) + "/" + LTRIM$(STR$(denom))


                    [This message has been edited by Jerry Fielden (edited July 04, 2001).]

                    Comment


                    • #11
                      Oops. Lance, I hadn't noticed the date of the original message.
                      The more recent post made the topic seem like a new one.
                      By the way, are messages in the archive forums conserved
                      indefinitely, or are they purged after a certain time?

                      As for reducing fractions, the VbToTheMax.com code doesn't
                      have the same limitations as some of the code I've seen posted.
                      It allows you to reduce fractions, and it can also convert a
                      floating point value to a clean-cut accurate fraction, using
                      a minimum of iterations. For instance:

                      DecToProperFrac(5655/5423, a, b, c)
                      would return 1 8/187 (in the values a, b, and c)


                      ------------------
                      Daniel Corbier
                      UCalc Fast Math Parser
                      http://www.ucalc.com
                      Daniel Corbier
                      uCalc Fast Math Parser
                      uCalc Language Builder
                      sigpic

                      Comment


                      • #12
                        We don't generally archive messages unless necessary. What with improvements to the
                        UBB forum software and moving to a much faster server, we have not had to archive
                        messages for some time...

                        ------------------
                        Tom Hanlin
                        PowerBASIC Staff

                        Comment


                        • #13
                          Sorry I react so late (and my bad English), but I bought PB 3.5 few days ago. I read that you want a routine for reducing fractions and mixed numbers. I saw all the routines you got, but my opinion is that you must try mine. The fastest and simpliest routine you need is based upon prime numbers (they can be divided only by 1 and themselves) but it will complicate the program too much, because you need a generator of prime numbers. If we ignore that routine, then this one is next to it by speed. The routine that Jerry Fielden sent to you is very near to what you want but it is as fast as a snail. Why? Let me analyze my routine.
                          If you input number 990 in line (03) and number 60 in line (04) - in lines (06) through (15) you will get 16 30 / 60. In other words you are "normalizing" the fraction. Numerator becomes smaller than denominator. In lines (16) - (23) you divide numerator and denominator only with number 2 because there is no further need for dividing with even numbers (they are not prime numbers). This is the first part of the acceleration, and this is the reason why, in line (25) in the FOR...NEXT loop, STEP is equal to 2. After line (23) the fraction is 16 15 / 30.
                          The real stuff is hidden in line (25). Instead to evaluate the FOR...NEXT loop to the value of denominator it is quite enough to evaluate to square root of it!!! If you need an explanation then take a piece of paper and write down all numbers from 3 to 100. Cross over all even numbers. Circle number 3 and after that cross over all numbers that are multiply of 3 (6, 9... 99). After that find the first number after 3 that is not crossed over (number 5) and then repeat the same (cross over numbers 10, 15... 95). Then try the same with number 7 and so on. You will find out that after number 10 (and 10 is square root of 100) there is no other numbers to cross over and all other numbers left are prime numbers!!! And, this is the algorithm for prime numbers routine. At the end the fraction is 16 1 / 2.

                          (01) DEFDBL A-Z
                          (02) CLS

                          (03) INPUT "Numerator: ", num
                          (04) INPUT "Denominator: ", denom
                          (05) PRINT

                          (06) IF num > denom THEN
                          (07) whole = FIX(num / denom)
                          (08) num = num MOD denom
                          (09) IF num = 0 THEN
                          (10) PRINT whole
                          (11) PRINT
                          (12) PRINT "END"
                          (13) END
                          (14) END IF
                          (15) END IF

                          (16) DO WHILE ind = 0
                          (17) ind = 1
                          (18) IF (num MOD 2) = 0 AND (denom MOD 2) = 0 THEN
                          (19) ind = 0
                          (20) num = num \ 2
                          (21) denom = denom \ 2
                          (22) END IF
                          (23) LOOP

                          (24) ind = 0

                          (25) FOR i = 3 TO SQR(denom) + 1 STEP 2
                          (26) IF i > denom THEN EXIT FOR
                          (27) DO WHILE ind = 0
                          (28) ind = 1
                          (29) IF (num MOD i) = 0 AND (denom MOD i) = 0 THEN
                          (30) ind = 0
                          (31) num = num \ i
                          (32) denom = denom \ i
                          (33) END IF
                          (34) LOOP
                          (35) NEXT i

                          (36) IF whole <> 0 THEN
                          (37) PRINT whole;" "; num; "/"; denom
                          (38) ELSE
                          (39) PRINT num; "/"; denom
                          (40) END IF

                          (41) PRINT
                          (42) PRINT "END"



                          ------------------

                          Comment


                          • #14
                            Sorry Petar,

                            Your code is kicking out the wrong answers. You may need to
                            modify it a little.

                            I entered 5655 for the Numerator and 5423 for the Denominator.
                            your answer is like the rest. 1 232/5423. The correct answer is
                            1 8/187.

                            MTIMER reports 600 microseconds using my code for that problem.
                            Thats not that slow. Of course my machine is not that slow either.

                            Just a little more and you will be there.



                            ------------------

                            Comment


                            • #15
                              Sorry Jerry. The mistake was in the IND variable that controls DO...LOOP. Maybe my program is longer but it is optimized as much as possible. I started programing on "Spectrum 64" were optimize code was necessary if you need real-time results. Nowadays speed maybe is not so critical but it is fine to make an effort and optimize your programs as much as possible.

                              Best regards,
                              Petar


                              DEFWRD A-Z
                              CLS

                              INPUT "Numerator: ", num
                              INPUT "Denominator: ", denom
                              PRINT

                              IF num > denom THEN
                              whole = FIX(num / denom)
                              num = num MOD denom
                              IF num = 0 THEN
                              PRINT whole
                              PRINT "END"
                              END
                              END IF
                              END IF

                              ind = 1
                              DO WHILE ind = 1
                              ind = 0
                              IF num MOD 2 = 0 AND denom MOD 2 = 0 THEN
                              ind = 1
                              num = num / 2
                              denom = denom / 2
                              END IF
                              LOOP

                              FOR i = 3 TO SQR(denom) STEP 2
                              IF i > denom THEN EXIT FOR
                              ind = 1
                              DO WHILE ind = 1
                              ind = 0
                              IF num MOD i = 0 AND denom MOD i = 0 THEN
                              ind = 1
                              num = num / i
                              denom = denom / i
                              END IF
                              LOOP
                              NEXT i

                              IF whole <> 0 THEN
                              PRINT whole; " "; num; "/"; denom
                              ELSE
                              PRINT num; "/"; denom
                              END IF

                              PRINT "END"



                              ------------------

                              Comment


                              • #16
                                Sorry Petar,

                                MTIMER is reporting that your progam to be slower.

                                To further check it, I Hard coded 5655 as num and 5423 as denom.
                                I wrapped it in a 1000 loop FOR NEXT and zeroed out all variables
                                at the bottom of the loop. I Booted straight to DOS and ran it
                                about 20 times. Your average speed was 39695 microseconds.

                                I did the same thing with my snail with an average speed of
                                38651.

                                You must have left something else out, I'm sure you will fix it.

                                ------------------




                                [This message has been edited by Jerry Fielden (edited May 21, 2001).]

                                Comment

                                Working...
                                X