Pretty simple i think, but how do I do it?
Printable View
Pretty simple i think, but how do I do it?
check this..
BTW an iteration loop is no means an effiecient way to do this.Code:Private Function IsFactor(Num, ofNum) As Boolean
'Checks If Num is A factor of ofNum
If Not Num = 0 Then
IsFactor = ofNum Mod Num
IsFactor = Not IsFactor
End If
End Function
Private Sub Command1_Click()
For i = -10 To 10
If IsFactor(i, 56) Then
List1.AddItem i
End If
Next
End Sub
still it's logical for smaller ranges of numbers.
Oh, OK...
I am working on some other code for factors and trinomials, so I will hopefully post that here
I assume that you are talking about finding all prime factors of a given number. For example: 29, 17, 3, & 3 are the factors of 4437.
An approach would be to start by using an array of known primes: 2, 3, 5, 7, 11, 13, 17 . . . .
Find all the primes you can via the array. If there are more primes to be found, try every odd number after the last prime in the array.
For a VB program, you could have the user put a number into a Text Box, which you would immediatley put into a List Box. The general approach would be to work with the number put into the List Box, replacing it with two factors. For the above example, the List Box would contain the following after each cycle of the program.If you find no primes, the original number is the sole contents of the List Box.
- 4437
- 3, 1479
- 3, 3, 439
- 3, 3, 17, 29
Note the following.I used the above general idea to write a factoring program for my HP48GX calculator. Instead of the List Box, I used the LIFO Stack provided by the calculator.
- You you never need try a factor greater than the square root of the number you are working with. When you get past the square root of the number, you are finished. This is how you know when to stop trying for more factors.
- Once you tried a number as a factor, you need not try that factor nor any smaller factor. This provides a rule for avoiding extra work.
mmmmkay, thanks, still a bit hazy about the idea, anybody else written a program for this?