BALA Posted February 3, 2007 Share Posted February 3, 2007 (edited) I made this program that will find all the factors of a number. It works fine when you use it the first couple of times, but then it shows the only factors as 1 and the number. You would have to exit and restart the program to find the factors again. So I'm trying to figure out why it works the first couple of times before failing. expandcollapse popup#include <GuiConstants.au3> #include <Math.au3> Opt("GUIOnEventMode", 1) GUICreate("BA-FACTOR", 200, 180) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUISetState() GUICtrlCreateLabel("Input number you would like to factor:", 10, 10) $input = GUICtrlCreateInput("", 60, 30, 80) $button = GUICtrlCreateButton("Factor", 60, 60, 80) GUICtrlSetOnEvent($button, "Factor") $label = GUICtrlCreateLabel("The factors are:", 10, 100, 140, 60) $yes = 0 $check = 2 Func CLOSEClicked() Exit EndFunc Func Factor() If $yes = 1 Then GUICtrlSetData($label, "The factors are:") $yes = 0 EndIf $number = GUICtrlRead($input) If $number =1 Then GUICtrlSetData($label, GUICtrlRead($label) & " 1") ElseIf $number = 0 Then GUICtrlSetData($label, GUICtrlRead($label) & " 0") ElseIf $number < 0 Then MsgBox(0 & 48, "Error", "BA-FACTOR can only factor Whole numbers.") Else GUICtrlSetData($label, GUICtrlRead($label) & " 1, " & $number) $square = Sqrt($number) If IsInt($square) Then $count = $square + 1 Else $count = Int($number/2) EndIf For $i = 3 to $count $result = _MathCheckDiv($number, $check) If $result = 1 Then $check = $check + 1 Else $number2 = $number/$check If $number2 = $check Then GUICtrlSetData($label, GUICtrlRead($label) & ", " & $number2) $check = $check + 1 Else GUICtrlSetData($label, GUICtrlRead($label) & ", " & $check & ", " & $number2) $check = $check + 1 EndIf EndIf Next EndIf $check = 2 ; Fix first problem by adding this $yes = 1 EndFunc While 1 sleep(10) WEnd EDIT: I think it may lie in the $count or $check variables EDIT EDIT: yeah I fixed the problem it was the $check variable OK, new problem now it seems to repeat when you put in some numbers like 30 and 28 Edited February 3, 2007 by BALA [font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com Link to comment Share on other sites More sharing options...
BALA Posted February 4, 2007 Author Share Posted February 4, 2007 (edited) I figured since it's been about a day, I'd bump this. Problem: For some reason my code works fine with some numbers, but for others it starts restating already stated factors. I think it has something to do with how I set up the For loop. Edited February 4, 2007 by BALA [font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com Link to comment Share on other sites More sharing options...
Kourath Posted February 4, 2007 Share Posted February 4, 2007 (edited) well, i was looking at your code, and at first glance i didn't really understand what you were doing, so i just rewrote it. I wrote it fairly quickly, and it is possible i made a mistake, or that the code could be further optimized. note: takes a long time for long numbers. #include <Array.au3> $NumberFactored = _Factor(30) _ArrayDisplay($NumberFactored, "Factored") Func _Factor($NumberToFactor) Dim $ReturnValue[1] Local $LastX = 0 Local $NumberOfFactors = 0 For $y = 1 to $NumberToFactor step 1 for $x = 1 to $NumberToFactor if Number($x*$y) = $NumberToFactor Then If $x = $y Then $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X $ReturnValue[0] = $NumberOfFactors ExitLoop(2) ElseIf $LastX = $y Then $ReturnValue[0] = $NumberOfFactors ExitLoop(2) Else $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X EndIf EndIf Next Next Return $ReturnValue EndFunc it returns an array, with array[0] being how many factors there are, and then array[1] and array[2] being the factors, and so on and so forth with 3+4 and 5+6 and 7+8 ect hope i helped. edit: fixed a bug i saw in my code where it would not return two multipliers that were the same (ex: 10 and 10 when factoring 100) Edited February 4, 2007 by Kourath Link to comment Share on other sites More sharing options...
BALA Posted February 4, 2007 Author Share Posted February 4, 2007 (edited) Yeah, that seemed work, I was originally going to use arrays, but i couldn't figure out how to put the factors in (I'm not that good with arrays). EDIT: OK, I tried implementing your code into my GUI. For some reason it won't display the factors. I have a gut-feeling that I'm doing something really stupid, but I can't figure it out. expandcollapse popup#include <Array.au3> #include <GuiConstants.au3> Opt("GUIOnEventMode", 1) GUICreate("BA-FACTOR", 200, 185) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUISetState() GUICtrlCreateLabel("Input number you would like to factor:", 10, 10) $input = GUICtrlCreateInput("", 60, 30, 80) $button = GUICtrlCreateButton("Factor", 60, 60, 80) GUICtrlSetOnEvent($button, "Factor") GUICtrlCreateLabel("The factors are:", 10, 100, 100, 20) $display = GUICtrlCreateEdit("", 10, 120, 180, 60, BitOR($ES_READONLY, $WS_VSCROLL, $ES_MULTILINE)) GUICtrlSetBkColor(-1, 0xFFFFFF) $check = 0 $num = 0 Func CLOSEClicked() Exit EndFunc Func Factor() $NumberToFactor = GUICtrlRead($input) Dim $ReturnValue[1] Local $LastX = 0 Local $NumberOfFactors = 0 For $y = 1 to $NumberToFactor step 1 for $x = 1 to $NumberToFactor if Number($x*$y) = $NumberToFactor Then If $x = $y Then $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X $ReturnValue[0] = $NumberOfFactors ExitLoop(2) ElseIf $LastX = $y Then $ReturnValue[0] = $NumberOfFactors ExitLoop(2) Else $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X EndIf EndIf Next Next Return $ReturnValue For $i = 0 to $NumberOfFactors If $num = 0 Then GUICtrlSetData($display, $ReturnValue[0]) $num = $num + 1 Else GUICtrlSetData($display, GUICtrlRead($display) & ", " & $ReturnValue[$num]) $num = $num + 1 EndIf Next $check = 1 $num = 0 EndFunc While 1 Sleep(10) WEnd EndFunc Edited February 4, 2007 by BALA [font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com Link to comment Share on other sites More sharing options...
Kourath Posted February 5, 2007 Share Posted February 5, 2007 at first glance there didn't appear to be anything wrong with the code. After some brief code-checking, I wasn't sure what was wrong, so i decided to start from scratch to see if i could get it to work. after some time, i managed to bang this out: expandcollapse popup#include <Array.au3> #include <GuiConstants.au3> Opt("GUIOnEventMode", 1) GUICreate("BA-FACTOR", 200, 185) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUISetState() GUICtrlCreateLabel("Input number you would like to factor:", 10, 10) $input = GUICtrlCreateInput("", 60, 30, 80) $button = GUICtrlCreateButton("Factor", 60, 60, 80) GUICtrlSetOnEvent($button, "Factor") GUICtrlCreateLabel("The factors are:", 10, 100, 100, 20) $display = GUICtrlCreateEdit("", 10, 120, 180, 60, BitOR($ES_READONLY, $WS_VSCROLL, $ES_MULTILINE)) GUICtrlSetBkColor(-1, 0xFFFFFF) Func CLOSEClicked() Exit EndFunc Func Factor() $NumberToFactor = GUICtrlRead($input) Dim $ReturnValue[1] Local $LastX = 0 Local $NumberOfFactors = 0 For $y = 1 to $NumberToFactor step 1 for $x = 1 to $NumberToFactor if Number($x*$y) = $NumberToFactor Then If $x = $y Then $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X $ReturnValue[0] = $NumberOfFactors ExitLoop(2) ElseIf $LastX = $y Then $ReturnValue[0] = $NumberOfFactors ExitLoop(2) Else $NumberOfFactors = number($NumberOfFactors + 1) _ArrayAdd ( $ReturnValue, $x) _ArrayAdd ( $ReturnValue, $y) $LastX = $X EndIf EndIf Next Next GUICtrlSetData($Display, $ReturnValue[0] & " factors:" & @CRLF) for $i = 1 to number($ReturnValue[0]*2) Step 2 GUICtrlSetData($Display, "(" & $ReturnValue[$i] & " * " & $ReturnValue[$i+1] & ") ", 1) Next EndFunc While 1 Sleep(10) WEnd as you can see, i just put it into a for loop which loops until the end of the array has been reached and all the values have been printed. It appears to be working well, however, as we all know, Murphy's laws have an uncanny way of ending up correct. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now