Results 1 to 14 of 14

Thread: for...to...finding factors - almost got it but....

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90
    Hi,

    My code below generates a random number from -20 to 20. When command2 is clicked, the whole number factors of that number generated are listed in Picture2.

    My problem is that I don't want factors to be repeated.

    For example, the random number generated in picture1, might be 6.

    When command2 is clicked the factors listed will be:

    -6 and -1
    -3 and -2
    -2 and -3
    -1 and -6
    1 and 6
    2 and 3
    3 and 2
    6 and 1

    You'll notice that factors are repeated, just in different order. Any ideas how I could have the above looking like:

    -6 and -1
    -3 and -2
    1 and 6
    2 and 3

    Thanks for any help! I include my code below.

    Dim number As Integer
    Dim x As Integer

    Private Sub Command1_Click()
    Randomize
    number = Int(Rnd * 40) - 20
    Picture1.Cls
    Picture1.Print number
    End Sub

    Private Sub Command2_Click()
    Picture2.Cls
    'Prevent backward For - To's like 7 to -7
    'if the number generated is less than 0, for example: -7
    If number < 0 Then
    'd will equal -7
    d = number
    'and e will equal --7, i.e: +7, so we go from -7 to 7 rather than
    '7 to -7, which For- To doesn't deal with
    e = -number
    Else:
    d = -number
    e = number
    End If
    For x = d To e
    'avoid division by 0 - gives an error
    If x = 0 Then
    x = 1
    End If
    'make sure we get a whole number factor
    If number Mod x = 0 Then
    'print all factors
    Picture2.Print x & " and " & number / x
    End If
    Next
    End Sub





  2. #2
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    Go from -round(number/2) to round(number/2). Maybe it will be a problem with the rounding if number is negative. If so use the Abs() function to get the absolut value.
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Actually go from -int(sqr(number/2)) to int(sqr(number/2)) and you'll avoid all double factors
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Wink

    when i understand your quetion, this will be an answer:

    when you ask a random number, its not really a random number, because all the 'random' numbers are listed on your pc, and will always come in the same order.
    What I do when i need a random number, i also pick a random number with the rnd function, but i multiply it with the system timer, and i / it trough the date, so i have every day, every time another number

    example

    Code:
    number = Int(Rnd * 40) - 20 
    number = number * Timer
    tmp = Date(Now)
    number = number / tmp
    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  5. #5
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    kedaman

    You cant take a square root from a negative number.

    You should not divide by two.

    It should maybe look like this.

    from -int(sqr(abs(number))) to int(sqr(abs(number)))

    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hehe, actually i can, but VB can't handle imaginary numbers I thought number was a positive number, the 20 he was talking about ;=)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    You are so right I had forgoten imaginary numbers ! So long since scool, twelve years and not a single imaginary number.
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  8. #8
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs up

    Everybody thanks,

    AKA, what is Abs? Absolute number? (And what is an absolute number again?)

    My code is working now, but I'd still like to know what's going on.

  9. #9
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    Abs function gives you the absoulte number IOW the positive number. Ex Abs(-6) => 6 and Abs(6) => 6.
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hehe about 1½ year for me, and imaginary numbers showed up last time i talked to sam. You live in sweden? Jag har inte varit där på ett tag, men det är inte så svårt att ta sig dit om man bor på Åland

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90

    Unhappy


    Hi there,

    Thanks for your help. I got my problem sorted with the code below but I hope you can help me with the next part- pick the correct factors from the group of factors.

    This is what my program does:

    It generates quadratic equations at random and the objective for the student is to find the factors of that equation.

    I generate the equations by a mathematical rule that I won't go into here - too complex. If you look at my code you'll see what my program does.

    Since you're talking so much about old days in school, you might remember a rule for solving a simple quadratic equation is: find two numbers that multiply to give c, and add to give b.

    For example, all equations can be written in the form: ax^2 + bx + c. a,b and c are generated at random, on the basis that the factos will be whole numbers.

    If the equation generated is:
    x^2+2x-8
    Factors are: (x-4)(x+2) (-4 and 2 multiply to give -8, add to give 2)

    x^2+ 3x-4
    Factors are: (x+4)(x-1) (4 and -1 multiply to give -4 and add to give 3)

    So, as you have helped me do, I can list the possible factors of the equation. Now I have to choose the correct factor, and the best way of doing this is by picking the two numbers that add to give b, thus elimating all the errors. But my program crashes every time I try it. If you could take the time to look at my code, I'd greatly appreciate it. In any case, thanks for your help!

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

    'Five command buttons:command1,command2,command3,command4
    'command5
    'two picture boxes, picture1 and picture2

    Option Explicit
    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    Dim x As Integer

    Dim bplus As String
    Dim cplus As String



    Private Sub Command1_Click()
    Dim factorsfound As Boolean
    'this is a mathematical rule. Roots of a quadratic
    'are always:

    'x= -b + or - Sqr (b^2-4ac)/2a

    Do

    a = 1
    b = randomfactors
    c = randomfactors

    bplus = IIf(b > 0, "+" & b & " ", b & " ")
    cplus = IIf(c > 0, "+" & c & " ", c & " ")

    If b ^ 2 - 4 * a * c >= 0 Then
    If b ^ 2 - 4 * a * c >= 0 Then
    If (b ^ 2 - 4 * a * c) = 1 Or (b ^ 2 - 4 * a * c) = 4 _
    Or (b ^ 2 - 4 * a * c) = 9 Or (b ^ 2 - 4 * a * c) = 16 _
    Or (b ^ 2 - 4 * a * c) = 25 Or (b ^ 2 - 4 * a * c) = 36 _
    Or (b ^ 2 - 4 * a * c) = 49 Or (b ^ 2 - 4 * a * c) = 64 _
    Or (b ^ 2 - 4 * a * c) = 81 Or (b ^ 2 - 4 * a * c) = 100 Then
    If (-b + Sqr(b ^ 2 - 4 * a * c)) Mod 2 * a = 0 Then
    If (-b - Sqr(b ^ 2 - 4 * a * c)) Mod 2 * a = 0 Then


    factorsfound = True

    End If
    End If
    End If
    End If
    End If

    Loop Until factorsfound

    Picture1.Cls
    Picture1.Print " Factorise: x² " & bplus & cplus

    Picture2.Cls
    Picture2.Print a & " " & b & " " & c

    End Sub


    Private Function randomfactors() As Integer
    Randomize
    randomfactors = Int(Rnd * 40) - 20
    If randomfactors = 0 Then
    randomfactors = 1 + Int(Rnd * 10)
    End If
    End Function


    Private Sub Command2_Click()
    Picture2.Cls
    Picture2.Print "Our factors will always be of the form (x+?)(x+?)."
    Picture2.Print
    Picture2.Print "The numbers that end up in the brackets will always"
    Picture2.Print "multiply to give " & c & " and add to give " & b & "."
    Picture2.Print "What numbers can you think of?"
    End Sub


    Private Sub Command3_Click()
    Picture2.Cls
    Picture2.Print "These pairs multiply to give " & c & ":"
    Picture2.Print
    For x = -Int(Sqr(Abs(c))) To Int(Sqr(Abs(c)))
    'avoid division by 0 - gives an error
    If x = 0 Then
    x = 1
    End If
    'make sure we get a whole number factor
    If c Mod x = 0 Then
    'print all factors
    Picture2.Print x & " and " & c / x
    End If
    Next
    Picture2.Print
    Picture2.Print "Which pair adds to give " & b & "?"
    End Sub
    'Program crashes here
    Private Sub Command4_Click()
    Dim addfactors As Boolean

    Picture2.Cls
    Do

    If x = 0 Then
    x = 1
    If x + (c / x) = b Then

    addfactors = True
    End If
    End If

    Loop Until addfactors
    Picture2.Print x & " " & c / x
    End Sub






  12. #12
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Christophe,

    You're going about the whole thing the wrong way mate, with all the questions start of with the answer and then work out the question.


    pick an answer in the form

    (ax - b)(cx - d) = 0


    then you can work out the equation

    (ac)x^2 - (ad + bc)x + (bd) = 0

    so have something like this

    Code:
    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    Dim d As Integer
    
    
    Public Sub getquestion()
    
    a = Fix(Rnd * 4) + 1
    b = Fix(Rnd * 9) + 1
    c = Fix(Rnd * 4) + 1
    d = Fix(Rnd * 9) + 1
    
    Form1.Pictureblackbrd.Cls
    Form1.Pictureblackbrd.Print 
    Form1.Pictureblackbrd.Print "We are given the equation"
    Form1.Pictureblackbrd.Print 
    Form1.Pictureblackbrd.Print CStr(a*c) & "x² - " & CStr((a * d) + (b * c)) & " x + " & CStr(b * d)
    Form1.Pictureblackbrd.Print 
    Form1.Pictureblackbrd.Print " Find the value of x."
    
    End Sub
    Then the rest is easy.

  13. #13
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    This thread show a for me as a system developer at an "inhouse" IT department a common problem. The users always comes with the question can you do this change to this list/app. You normaly can do it but you always will do a better solution if you ask the question "Wath do you want to achive ?".

    Christophe's last mail that the one he realu wanted help with. But he of course learnt more from all answers this way .
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thumbs up I just digged up my old mathbook again and...


    yep, for an equation
    ax^2+bx+c=0

    where the discriminand, the part under the root is:
    D=b^2-4ac

    So you have three formulas depending on D
    Code:
    if D>0 then
      x1=(-b-sqr(D)/2a
      x2=(-b+sqr(D)/2a
    elseif D=0
      x1i=sqr(-D)/2a 'imaginary 1
      x2i=-sqr(-D)/2a 'imaginary 2
      xr=sqr(-D)/2a 'real
    else
      x=-b/2a
    end if
    If you have a negative discriminand, you get two roots, wich should be their conjugate, (hmm is this the correct term?) imaginary values.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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