[t][TT #1119] Convert t/op/bitwise.t to PIR
[parrot.git] / examples / benchmarks / primes.pasm
blobb6d0cb6f671ba683be3bb18f4826d0708efa0726
1 # Copyright (C) 2001-2009, Parrot Foundation.
2 # $Id$
4 =head1 NAME
6 examples/benchmarks/primes.pasm - Calculate prime numbers < 5000
8 =head1 SYNOPSIS
10     % time ./parrot examples/benchmarks/primes.pasm
12 =head1 DESCRIPTION
14 Calculates all the prime numbers up to 5000 and prints out the number
15 of primes, the last one found, and the time taken.
17 =cut
19     # P1 holds the number we're currently checking for primality
20     new     P1, 'Integer'
21     set     P1, 1
23     # P2 holds the highest number we want to check for primality
24     new     P2, 'Integer'
25     set     P2, 5000
27     new     P6, 'Integer'
28     set     P6, 0
29     print   "N primes up to "
30     print   P2
31     print   " is: "
32     time    N10
34     # P1 counts up to P2
35     # P3 counts from 2 up to P4 (P1/2)
36     new     P3, 'Integer'
38 REDO:
39     set     P3, 2
40     div     P4, P1, 2
41     # Check if P3 is a factor of P1
42 LOOP:
43     cmod    P5, P1, P3
44     if      P5, OK
46     # We've found a factor, so it can't be a prime and
47     # we can skip right out of this loop and to the next number
48     branch  NEXT
49 OK:
50     inc     P3
51     lt      P3, P4, LOOP
52     # We haven't found a factor so it must be a prime
53     inc     P6
54     set     P7, P1
55     # print I1
56     # print "\n"    # to get them all
58     # Move on to the next number
59 NEXT:
60     inc     P1
61     le      P1, P2, REDO
62     time    N11
63     say     P6
64     print   "last is: "
65     say     P7
66     sub     N11, N10
67     print   "Elapsed time: "
68     say     N11
69     end
71 =head1 SEE ALSO
73 F<examples/benchmarks/primes.c>,
74 F<examples/benchmarks/primes_i.pasm>,
75 F<examples/benchmarks/primes.pl>,
76 F<examples/benchmarks/primes.rb>,
77 F<examples/benchmarks/primes2_p.pasm>,
78 F<examples/benchmarks/primes2.c>,
79 F<examples/benchmarks/primes2.pir>,
80 F<examples/benchmarks/primes2.py>.
82 =cut
84 # Local Variables:
85 #   mode: pir
86 #   fill-column: 100
87 # End:
88 # vim: expandtab shiftwidth=4 ft=pir: