Wednesday, December 19, 2018

HP Prime and TI-84 Plus: Sieve of Eratosthenes

HP Prime and TI-84 Plus: Sieve of Eratosthenes

Introduction

The program SIEVEMIN shows a miniature version of the famous Sieve of Eratosthenes.  The Sieve of Eratosthenes is a Greek algorithm that determines prime numbers from 2 to N through eliminating multiplies.

Algorithm:

1.  List all integers from 1 to n.  Of course, you choose what n is. 

2.   1 is not a prime number, hence eliminate 1. 

3.  Start with 2.  Eliminate multiple of 2, but not 2 itself.

4.  Then do step 3, but with multiples of 3, but not 3 itself.

5.  Then do step 5, but with multiples of 5, but not 5 itself. 

6.  Continue on with the next available number until there are no multiples remaining.  At the end, you have all the prime numbers from 1 to n.

Most sieves will demonstrate the algorithm from 1 to 100.  However, to fit the calculator screens, SIEVEMIN finds all the prime numbers from 1 to 49. 

HP Prime Program: SIEVEMIN 



sub1(); // subroutine

EXPORT SIEVEMIN()
BEGIN
// 2018-12-18 EWS
// Mini Sieve
HFormat:=0; // standard
PRINT();
PRINT("Mini Sieve");
WAIT(1);
LOCAL M0,T,R,C,K;
T:=1;
M0:=MAKEMAT(0,7,7);

FOR R FROM 1 TO 7 DO
FOR C FROM 1 TO 7 DO
M0(R,C):=T;
T:=1+T;
END;
END;

sub1(M0);

M0(1,1):=0;
FOR K FROM 2 TO 7 DO
FOR R FROM 1 TO 7 DO
FOR C FROM 1 TO 7 DO
IF FP(M0(R,C)/K)==0 AND 
M0(R,C)>K THEN
M0(R,C):=0;
sub1(M0);
END;
END;
END;
END;

// end of program

END;

sub1(M1)
BEGIN
PRINT();
LOCAL I;
FOR I FROM 1 TO 7 DO
PRINT(row(M1,I));
END;
WAIT(0.5);
END;

TI-84 Plus CE Program:  SIEVEMIN



"2018-12-11 EWS"
Disp "MINI SIEVE OF","ERATOSHENES","TI-84 PLUS CE"
Float
Wait 1
{7,7}→dim([J])
1→T
For(R,1,7)
For(C,1,7)
T→[J](R,C)
1+T→T
End
End
ClrHome
Disp [J]
Wait 1

0→[J](1,1)
For(K,2,7)
For(R,1,7)
For(C,1,7)
If fPart([J](R,C)/K)=0 and [J](R,C)>K
Then
0→[J](R,C)
ClrHome
Disp [J]
Wait .5
End
End
End
End

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Spotlight: TI-55 from 1977

Spotlight: TI-55 from 1977 Welcome to a special Monday Edition of Eddie’s Math and Calculator Blog. Today, we have the origi...