|
-
May 25th, 2004, 05:58 AM
#1
Thread Starter
New Member
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!
-
May 25th, 2004, 06:08 AM
#2
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.
-
May 25th, 2004, 06:28 AM
#3
Thread Starter
New Member
Thanks, my english is not very good, im portuguese. Sorry.
-
May 25th, 2004, 08:14 AM
#4
PowerPoster
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.
-
May 25th, 2004, 08:51 AM
#5
Thread Starter
New Member
Thanks,
Its more dificult than the shortest common multiple... i guess.
Sorry about the english its because im portuguese
-
May 25th, 2004, 08:59 AM
#6
PowerPoster
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.
-
May 25th, 2004, 09:25 AM
#7
Thread Starter
New Member
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.
-
May 25th, 2004, 10:09 AM
#8
Re: Re: maximum common divider
I don't live here any more.
-
May 25th, 2004, 10:46 AM
#9
PowerPoster
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.
-
May 25th, 2004, 11:26 AM
#10
PowerPoster
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.
-
May 25th, 2004, 11:34 AM
#11
PowerPoster
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.
-
May 25th, 2004, 06:19 PM
#12
Thread Starter
New Member
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!
-
May 26th, 2004, 11:30 AM
#13
PowerPoster
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Sorted = False
Dim myArray As New ArrayList
Dim iCheckStatus As Integer *****
Dim myValue As Integer
Dim iCount As Integer
Dim iCount1 As Integer *****
For iCount = 0 To 99
myValue = CInt(Int((1000 * Rnd()) + 1))
For iCount1 = 1 To iCount *****
If iCount > 0 Then *****
If myArray(iCount1 - 1) = myValue Then *****
iCount = iCount - 1 *****
iCheckStatus = 1 *****
Exit For *****
End If *****
End If *****
Next *****
If iCheckStatus = 0 Then *****
myArray.Add(myValue) *****
End If *****
iCheckStatus = 0 *****
Next
myArray.Sort()
For iCount = 0 To 99
ListBox1.Items.Add(myArray(iCount))
Next
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.
-
May 26th, 2004, 11:53 AM
#14
Thread Starter
New Member
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?
-
May 27th, 2004, 05:48 AM
#15
PowerPoster
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.
-
May 28th, 2004, 09:45 AM
#16
I don't live here any more.
-
May 28th, 2004, 09:48 AM
#17
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.
-
May 28th, 2004, 10:58 AM
#18
PowerPoster
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.
-
May 28th, 2004, 11:59 AM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|