Answered by:
Small Basic for Factoring
Question

estou querendo fazer um programa no Small Basic para Fatorar um número digitado pelo usuário. Quero que sejam apresentados todos os fatores, quer repetidos ou não.
Até consegui fazer com que sejam apresentados os fatores só que, no caso de fatores que se repetem o programa que fiz não mostra a repetição.
Ex.: número = 100 resultado que consigo: 2 5
resultado que desejo :2 2 5 5
I'm wanting to do a program on Small Basic for Factoring a number entered by the user.I want them to appear all factors, whether or not repeated.
Even managed to make the factors are presented only in the case of factors that arerepeated that the program did not show the replay.
Example: number = 100 that can result: 2 5
result I want: 2 2 5 5____________________________________________________________________________________________________________________________________________________
TextWindow.Write("Digite o número ")
n = TextWindow.ReadNumber()
For i = 2 to n
primo ="True"
VerificarPrimo()
If (primo = "True") Then
If (Math.remainder (n,i) = 0 ) Then
TextWindow.WriteLine(i)
n = n/i
EndIf
EndIf
endfor
Sub VerificarPrimo
For j = 2 To Math.SquareRoot(i)
If (Math.Remainder(i,j) = 0 ) Then
primo ="False"
Goto FimLoop
EndIf
EndFor
fimloop:
endsub
 Edited by Sergio74132 Monday, February 14, 2011 9:07 PM
Sunday, February 13, 2011 11:43 AM
Answers

Sergio, your code looks good  you spotted that you only need to check upto the square root etc.
The issue is that when you find a factor you divide by it to update n, but then you move onto the next possible divisor i and don't check for multiple values. Here it is modified:
TextWindow.Write("Digite o número ")
n = TextWindow.ReadNumber()
For i = 2 to n
primo ="True"
VerificarPrimo()
If (primo = "True") Then
If (Math.remainder (n,i) = 0 ) Then
TextWindow.WriteLine(i)
n = n/i
i = i1 ' Retry this divisor
EndIf
EndIf
endfor
Sub VerificarPrimo
For j = 2 To Math.SquareRoot(i)
If (Math.Remainder(i,j) = 0 ) Then
primo ="False"
Goto FimLoop
EndIf
EndFor
fimloop:
endsub
Good luck.PS if you modify your question the thread isn't flagged as anything changed so I only noticed the addition you made by chance five days later. Best to add a reply when you continue the thread discussion.
Also, if you are interested, here is something I was playing with trying to create factors of large numbers as efficiently as possible using SmallBasic. Import VZS962.
Friday, February 18, 2011 9:00 PM
All replies

There are seveal ways this could be done, perhaps using prime numbers. Plase show the code you have that finds the factors but not repeated factors to get more suggestions.Sunday, February 13, 2011 2:28 PM

Sergio, your code looks good  you spotted that you only need to check upto the square root etc.
The issue is that when you find a factor you divide by it to update n, but then you move onto the next possible divisor i and don't check for multiple values. Here it is modified:
TextWindow.Write("Digite o número ")
n = TextWindow.ReadNumber()
For i = 2 to n
primo ="True"
VerificarPrimo()
If (primo = "True") Then
If (Math.remainder (n,i) = 0 ) Then
TextWindow.WriteLine(i)
n = n/i
i = i1 ' Retry this divisor
EndIf
EndIf
endfor
Sub VerificarPrimo
For j = 2 To Math.SquareRoot(i)
If (Math.Remainder(i,j) = 0 ) Then
primo ="False"
Goto FimLoop
EndIf
EndFor
fimloop:
endsub
Good luck.PS if you modify your question the thread isn't flagged as anything changed so I only noticed the addition you made by chance five days later. Best to add a reply when you continue the thread discussion.
Also, if you are interested, here is something I was playing with trying to create factors of large numbers as efficiently as possible using SmallBasic. Import VZS962.
Friday, February 18, 2011 9:00 PM