Jump to content

Count in factors: Difference between revisions

/* Added PureBasic
(Simplified)
(/* Added PureBasic)
Line 156:
19: 19
20: 2 * 2 * 5</pre>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure Factorize(Number, List Factors())
Protected I = 3, Max
ClearList(Factors())
While Number % 2 = 0
AddElement(Factors())
Factors() = 2
Number / 2
Wend
Max = Number
While I <= Max And Number > 1
While Number % I = 0
AddElement(Factors())
Factors() = I
Number / I
Wend
I + 2
Wend
EndProcedure
 
If OpenConsole()
NewList n()
For a=1 To 20
text$=RSet(Str(a),2)+"= "
Factorize(a,n())
If ListSize(n())
ResetList(n())
While NextElement(n())
text$ + Str(n())
If ListSize(n())-ListIndex(n())>1
text$ + "*"
EndIf
Wend
Else
text$+Str(a) ; To handle the '1', which is not really a prime...
EndIf
PrintN(text$)
Next a
EndIf</lang>
<pre> 1= 1
2= 2
3= 3
4= 2*2
5= 5
6= 2*3
7= 7
8= 2*2*2
9= 3*3
10= 2*5
11= 11
12= 2*2*3
13= 13
14= 2*7
15= 3*5
16= 2*2*2*2
17= 17
18= 2*3*3
19= 19
20= 2*2*5</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.