FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
smallbasic.github.io/samples/node/1240.bas at master · smallbasic/smallbasic.github.io · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
smallbasic
/
smallbasic.github.io
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
smallbasic.github.io
/
samples
/
node
/
1240.bas
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (95 loc) · 3.4 KB
Breadcrumbs
smallbasic.github.io
/
samples
/
node
/
1240.bas
Copy path
File metadata and controls
102 lines (95 loc) · 3.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'
First Factors MGA.BAS 10/19/14
'
modified 10/24/14 with screen displays improvement a neat general code tip!
'
for email to SmallBASIC crew
'
I was challenged/inspired by the featured Primes.BAS code in SmallBASIC forum 10/18/14
'
First Factors works from a different angle producing an array that is actually useful
'
for finding more than just prime numbers. Use it to factor any number up to...
'
topN, I left it at 1223 because that is the 200th prime found with Primes.BAS
'
on my machine and system I get .002+ or .004+ secs for 200 primes but .02+ something with Primes.BAS
'
of course I've gone up to 100,000 for TopN with .2+ secs results
'
The algorithm is based on Sieve of Eratostheses,
'
old as Greek Geometry with some prime number study added.
'
Ha! looking up how to spell Sieve of Eratostheses I learn of Euler's Sieve (Wiki)
'
n+1... there is always more that one can do, if one can do something!
'
THANK YOU SmallBASIC crew and the Open Code Spirit - MGA
'
'
This program code is a great first tool for Number Theory Study
'
I think it's pretty good for teaching SmallBASIC as well
'
option base
1
'
===================== topN =1223 for first 200 primes
'
===================== topN =1000000 2.5+secs to load, 3.7+ to load and count
topN
=
1223
testlimitN
=
sqr
(
topN
)
dim
ffA
(
topN
+
6
)
'
In first FOR loop:
'
you could go nuts performance wise and build patterns with 30 or 210 which are prime factorials
'
here is just 6=2*3 which is an improvement on just 2 odds and evens.
'
Prime factorials: 30=2*3*5 and 210=2*3*5*7... these create ever expanding and overlapping patterns
tock
=
ticks
for
i
=
0
to
topN
step
6
ffA
(
i
+
1
)=
1
:
ffA
(
i
+
2
)=
2
:
ffA
(
i
+
3
)=
3
:
ffA
(
i
+
4
)=
2
:
ffA
(
i
+
5
)=
1
:
ffA
(
i
+
6
)=
2
next
ffa
(
2
)=
1
:
ffa
(
3
)=
1
'
fix first two primes
for
pcand
=
5
to
testlimitN
step
2
if
ffA
(
pcand
)=
1
then
for
i
=
pcand
+
pcand
to
topN
step
pcand
if
ffA
(
i
)=
1
then
ffA
(
i
)=
pcand
:
i
=
i
+
pcand
'
booster shot .02 secs on 100,000
next
fi
next
tic
=
ticks
?
print
topN;
"
First Factors Array Complete in
"
;(
tic
-
tock
)/
tickspersec
;
"
secs.
"
?
pcount
=
0
for
i
=
2
to
topN
if
ffA
(
i
)=
1
then
pcount
=
pcount
+
1
next
tic
=
ticks
print
"
In first
"
;topN;
"
numbers there are
"
;pcount;
"
Primes.
"
?
print
"
In case a prime is not a prime until it is counted as one:
"
?
"
It took
"
; (
tic
-
tock
)/
tickspersec
;
"
secs to load First Factor Array and count it's Primes.
"
?
Print
"
press any
"
?
pause
'
the following demo's a neat way to display tons of screens of data or file
'
and stop it where you want, great general programming tip!
cls
for
i
=
2
to
topN
if
ffa
(
i
)=
1
then
print
i,
count
=
count
+
1
if
frac
(
count
/
10
)=
0
then
print
if
frac
(
count
/
200
)=
0
then
?:
Input
"
** enter s to stop display, just enter to continue **
"
;s
if
ucase
(
s
)=
"
S
"
then
exit
cls
fi
fi
next
'
now let's use our array for something useful besides prime numbers
label FactorNumber
?
?
"
Enter a number up to
"
+
TopN
+
"
to factor ( <2 to quit )
"
;
input
xn
if
xn
>=
2
and
xn
<=
topN
then
? xn;
"
factors are:
"
,
while
ffA
(
xn
)<>
1
print
ffa
(
xn
),
xn
=
xn
/
ffa
(
xn
)
wend
print
xn
?
else
'
I have learned with SmallBASIC we need to say when we are done
'
because in SmallBASIC we can go back and look inside our code while
'
the program is running, its nice feature but can mess you up if you
'
forget the program is still running
?:?
"
That's all Folks!
"
END
fi
goto
FactorNumber
'
now use this code to do your own number studies
Back
|
FazBrowse Home
|
New Git URL