-
Notifications
You must be signed in to change notification settings - Fork 138
/
primes.pasm
92 lines (72 loc) · 1.82 KB
/
primes.pasm
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
# Copyright (C) 2001-2009, Parrot Foundation.
=head1 NAME
examples/benchmarks/primes.pasm - Calculate prime numbers < 5000
=head1 SYNOPSIS
% time ./parrot examples/benchmarks/primes.pasm
=head1 DESCRIPTION
Calculates all the prime numbers up to 5000 and prints out the number
of primes, the last one found, and the time taken.
=cut
.pcc_sub :main main:
# P1 holds the number we're currently checking for primality
new P1, 'Integer'
set P1, 1
# P2 holds the highest number we want to check for primality
new P2, 'Integer'
set P2, 5000
new P6, 'Integer'
set P6, 0
new P7, 'Integer'
print "N primes up to "
print P2
print " is: "
time N10
# P1 counts up to P2
# P3 counts from 2 up to P4 (P1/2)
new P3, 'Integer'
REDO:
set P3, 2
div P4, P1, 2
# Check if P3 is a factor of P1
LOOP:
.loadlib 'math_ops' # cmod is a dynop
cmod P5, P1, P3
if P5, OK
# We've found a factor, so it can't be a prime and
# we can skip right out of this loop and to the next number
branch NEXT
OK:
inc P3
le P3, P4, LOOP
# We haven't found a factor so it must be a prime
inc P6
assign P7, P1
# print I1
# print "\n" # to get them all
# Move on to the next number
NEXT:
inc P1
le P1, P2, REDO
time N11
say P6
print "last is: "
say P7
sub N11, N10
print "Elapsed time: "
say N11
end
=head1 SEE ALSO
F<examples/benchmarks/primes.c>,
F<examples/benchmarks/primes_i.pasm>,
F<examples/benchmarks/primes.pl>,
F<examples/benchmarks/primes.rb>,
F<examples/benchmarks/primes2_p.pasm>,
F<examples/benchmarks/primes2.c>,
F<examples/benchmarks/primes2.pir>,
F<examples/benchmarks/primes2.py>.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: