Results 1 to 19 of 19

Thread: maximum common divider

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13

    Question maximum common divider

    Hi,

    Could any one tell me the code, to finde the maximum common divider between two numbers in Visual Basic.Net.
    Thanks!

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    look up "Greatest Common Divisor code" on google, you'll get loads of different versions.

    Some are fast some are not.
    I don't live here any more.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13
    Thanks, my english is not very good, im portuguese. Sorry.

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: maximum common divider

    Originally posted by plionheart
    Hi,

    Could any one tell me the code, to finde the maximum common divider between two numbers in Visual Basic.Net.
    Thanks!
    The correct term for maximum common dividor is "Highest Common Multiple" but we will stick with your term "divider"

    If you want the satisfaction of writing your own code, here are a few pointers:

    I assume your "divider" has to be a whole number.

    1. Find the highest "divider" of the first number. "A"
    2. Find the highest divider of the second number. "B"
    3. Compare the two. (They cannot be equal at this stage unless the original numbers are the same).
    4. Assuming "A" is higher than "B" find the next highest divider of "A" and compare it to "B" repeating this process until a result is obtained.

    To find the highest divider of an odd number start by dividing it by 3 and increment by 2 each loop. A purist might use only the prime numbers but that would greatly increase the coding required.

    To find the highest divider of an even number then you begin at 2 and increment by 1 each time.

    (Hint: use a function to which you pass the parameters of the number; the number at which to commence the division; the incremental step. Store the return and repeat for the other number).

    Let us know how you get on.
    Last edited by taxes; May 25th, 2004 at 08:18 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13
    Thanks,
    Its more dificult than the shortest common multiple... i guess.
    Sorry about the english its because im portuguese

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by plionheart
    Thanks,
    Its more dificult than the shortest common multiple... i guess.
    Sorry about the english its because im portuguese
    Not really. The principle is the same but whereas with the Lowest Common Multiple you simply start to divide by 2 (or 3) and stop when you find a match, with the Highest Common Multiple after starting by dividing by 2 (or 3) you then work with the result of dividing by 2 (or 3).

    Please don't apologise for language problems. Your English is better than my French or German and I know nothing about any other language

    Please tell me if you need any other help.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13
    I am to take off a VB.Net course, we are in a phase of learning of these mathematical problems. Another one of the problems was to see if one number is prime or not.(do you know how to do it?)
    But im not very good at mathe so i've got to get some help.

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Re: maximum common divider

    Originally posted by taxes
    The correct term for maximum common dividor is "Highest Common Multiple" but we will stick with your term "divider"
    I hate to be picky, but its not you know .

    "Greatest Common Divisor" (GCD) if the right term. DEK says so, and he knows.

    It would be unthinkable that you would dare contradict the Knuthmeister
    I don't live here any more.

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi wossname,

    " hate to be picky, but its not you know .

    "Greatest Common Divisor" (GCD) if the right term. DEK says so, and he knows.

    It would be unthinkable that you would dare contradict the Knuthmeister "


    Gloves off here

    All numbers, even prime numbers, are made up of multipliers, not devisors. I say that it is Highest Common Multiplier (But unfortunately I can't find it in my dictionary ) Who is DEK

    Still, things have changed since my schooldays (1952) so maybe arithmetic has as well Or are you referring to the Yankee (or Dixie) version
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by plionheart
    I am to take off a VB.Net course, we are in a phase of learning of these mathematical problems. Another one of the problems was to see if one number is prime or not.(do you know how to do it?)
    But im not very good at mathe so i've got to get some help.
    If you are learning VB.NET and this is one of your assignments, I really should not give you the actual code. What I can do is help you with the mathematical side.

    To find a list of prime numbers, you just have to take each number and see if it can be divided by any number, other than 1 or itself. Therefore, you can ignore :

    1. ALL even numbers (as they can all be divided by at least 2); (don't forget numbers ending in 0 are even numbers)

    2. All numbers ending in 5 (except 5 itself) as they can all be divided by 5;

    3. Checking any number not ending in 5 to see if it divides by 5.

    4. Checking even devisors as they will ONLY divide into even numbers.

    When checking each number, you do not need to go past the point where the devisor you have reached multiplied by 3 exceeds the number you are checking, as you will have already carried out that check in reverse. e.g. if you are checking the number 47, once you have tried 1, 3, 7, 9, 11, 13, 17, you need go no further as 3 * 17 = 51 which is greater than 47 and you know that no number multiplied by 1 or 2 will qualify.

    Let me know if you want any more pointers on this.
    Try and code it and come back with your results.

    I will post again shortly on the Higest Common Multiplier problem.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi plionheart,

    Did you indicate that you have already written a project to find what you described as "The shortest common multiple".? If so, post your code and we can look at the difference required.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13
    I don´t have it here with me but i will send you later.

    But now i have somethig that i can´t resolve and i need your help if you don´t mind.

    I have an Array from 0 to 99, and I want to generate random numbers from 0 to 1000 inside this Array, and I want to discover the three higher numbers and the lowest number that was randomize generated.

    If you could help me with some code samples i would be very thankfull, because i dont understand some words in english of your explanation, sow i can´t understand every thing you say to me when you´re explaning something.

    Thank's!

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Just this once. Code to view 100 random numbers between 1 & 1000. This code eliminates duplicate numbers. If you don't mind numbers being duplicated then miss out the code marked "*****"

    Put a listbox on your form to view the numbers

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         ListBox1.Sorted = False
    3.         Dim myArray As New ArrayList
    4.         Dim iCheckStatus As Integer                                 *****
    5.         Dim myValue As Integer
    6.         Dim iCount As Integer
    7.         Dim iCount1 As Integer                                         *****
    8.          For iCount = 0 To 99
    9.             myValue = CInt(Int((1000 * Rnd()) + 1))
    10.                For iCount1 = 1 To iCount                                    *****
    11.                 If iCount > 0 Then                                            *****
    12.                     If myArray(iCount1 - 1) = myValue Then      *****
    13.                         iCount = iCount - 1                                  *****
    14.                         iCheckStatus = 1                                     *****
    15.                         Exit For                                                    *****
    16.                     End If                                                          *****
    17.                 End If                                                              *****
    18.             Next                                                                    *****
    19.             If iCheckStatus = 0 Then                                     *****
    20.                 myArray.Add(myValue)                                    *****
    21.             End If                                                                  *****
    22.             iCheckStatus = 0                                                *****
    23.         Next
    24.  
    25.         myArray.Sort()
    26.         For iCount = 0 To 99
    27.             ListBox1.Items.Add(myArray(iCount))
    28.         Next
    29.     End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    13
    Thank's, this code will help me a lot to resolve the worst part.
    But my Teacher is crazy, because i can´t use the sort function to aligne the numbers, and than he wants us to find the 3 highest numbers and the first number, that was randomized created.
    But thank´s a lot, by the way... where are you come from?

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by plionheart
    Thank's, this code will help me a lot to resolve the worst part.
    But my Teacher is crazy, because i can´t use the sort function to aligne the numbers, and than he wants us to find the 3 highest numbers and the first number, that was randomized created.
    But thank´s a lot, by the way... where are you come from?


    If you mean that you are not allowed to use the Sorted property of a control then you will have to use another one dimensional array with 101 elements. Loop through the arraylist picking up the value of each element one at a time. Then loop through the new array until you locate a value HIGHER than the one you are holding. Move the contents of that and all HIGHER element numbers up one element and insert the value you are holding into the element just vacated. When finished, add the contents of the new array to the ListBox, making sure that the sorted property is set to false.

    The 3 highest numbers will be the last three in the list.

    The first number generated willl be found in the original arraylist at element (0)

    You COULD avoid using another array and actually sort the arraylist using the same principle, but the coding would be more difficult and you would have to make a note of the first random number generated.

    EDIT:
    In the above sorting method, you do need to have a number available which is higher than any other possible number. Therefore make sure your new array is dimensioned to 100 (not 99) and that before you begin sorting element 0 contains the number 1001
    Last edited by taxes; May 27th, 2004 at 08:05 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Originally posted by taxes
    Who is DEK
    Are you serious?

    Donald Edwin Knuth: author of "TAOCP"

    Now don't tell me you haven't heard of that.

    You should have a copy in the "Programming" section of your own private library (mine's above my bed) out of direct sunlight and bathing in rose petals and gentle classical music.

    If you still insist on be ignorant I'm on about "The Art of Computer Programming" 3 volume box set.

    The term divisor refers to the fact that it divides both numbers evenly. Numbers aren't "made up of" anything. If it was called Greatest Common Multiplyer, that would imply that both input numbers could be multiplied by the output number to have some relevant meaning, and in this context this is not the case.

    Regards

    Wossname
    Stuck-up pedant to the stars!
    I don't live here any more.

  17. #17
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    If we were going to be mega pedantic then it would be called Greatest Common Factor, but it isnt called that, so there.
    I don't live here any more.

  18. #18
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Wossname,

    To settle this argument I will pop along to the cemetary and ask my old maths teacher - it may be a long wait for his response Bye
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  19. #19
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    I can lend you a spade if you like.
    I don't live here any more.

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