Results 1 to 7 of 7

Thread: How to find ratio between 2 numbers

  1. #1

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562

    How to find ratio between 2 numbers

    How to find ratio between 2 numbers. Example: Ratio of 12 & 8 is 3:2
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  2. #2
    Si_the_geek
    Guest
    Here you go, it simply divides both numbers by 1,2.. up to half the value of the first number (the maximum possible divider). if both numbers can be divided exactly by the loop value then they are stored.. easy!
    VB Code:
    1. val_1 = 12
    2.   val_2 = 8
    3.  
    4.   For i = 1 To (val_1 \ 2)
    5.     If (val_1 / i) = (val_1 \ i) And _
    6.        (val_2 / i) = (val_2 \ i) Then
    7.        min_1 = (val_1 / i)
    8.        min_2 = (val_2 / i)
    9.     End If
    10.   Next i
    11.  
    12.   MsgBox CStr(min_1) & ":" & CStr(min_2)

  3. #3
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    up to half the value of the first number (the maximum possible divider).
    Why only up to half the first number?
    - If at first you dont succeed, then give up, cause you will never will!

  4. #4
    Si_the_geek
    Guest
    Why only up to half the first number?
    because anything higher cannot possibly return a whole number,
    BUT you have reminded me of a minor omission, you should also allow for dividing by the number itself! do'h!
    eg: 8 / 8 = 1 (gimme a few seconds to update the code!)

    ideally you should only check up to half of the lowest value rather than just the first (and there are other ways of reducing the workload), but unless you are working on huge numbers the speed isn't that important.

  5. #5
    Si_the_geek
    Guest
    better version:
    VB Code:
    1. val_1 = 15
    2.   val_2 = 120
    3.  
    4.   If val_1 < val_2 Then
    5.     min_val = val_1
    6.   Else
    7.     min_val = val_2
    8.   End If
    9.  
    10.   If (val_1 / min_val) = (val_1 \ min_val) And _
    11.      (val_2 / min_val) = (val_2 \ min_val) Then
    12.      min_1 = (val_1 / min_val)
    13.      min_2 = (val_2 / min_val)
    14.   Else
    15.     min_1 = val_1
    16.     min_2 = val_2
    17.     For i = (min_val \ 2) To 1 Step -1
    18.       If (val_1 / i) = (val_1 \ i) And _
    19.          (val_2 / i) = (val_2 \ i) Then
    20.          min_1 = (val_1 / i)
    21.          min_2 = (val_2 / i)
    22.          Exit For
    23.       End If
    24.     Next i
    25.   End If
    26.  
    27.   MsgBox CStr(min_1) & ":" & CStr(min_2)

  6. #6

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    Thank you
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  7. #7
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    Ah your right Si, thanks!
    - If at first you dont succeed, then give up, cause you will never will!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width