Showing posts with label primes. Show all posts
Showing posts with label primes. Show all posts

Saturday, December 9, 2023

Swiss Micros DM32 and Casio fx-4000P: Prime Factorization

Swiss Micros DM32 and Casio fx-4000P:   Prime Factorization



Introduction 



On March 22, 2017, I posted a prime factorization program for the HP 15C:


https://edspi31415.blogspot.com/2017/03/hp-15c-prime-factorization.html



Today's blog is a port for the:


* Swiss Micros DM32 (and HP 32SII family)

* Casio fx-4000P



Swiss Micros DM32 Code:  Prime Factorization



Instructions:


1.  Enter a positive integer on the X stack. 

2.  Run the program (XEQ P) and a prime factor is displayed.  Press [ R/S ] to continue.  At this point the calculator is set to FIX 0 mode.   Repeat step 2.

3.  The program ends when the integer reappears.  The calculator is reset to FIX 4.  


3 labels are needed.



P01  LBL P

P02  FIX 0

P03  STO B

P04  STO A

P05  2

P06  STO F


B01  LBL B

B02  RCL A

B03  RCL÷ F

B04  ENTER

B05  FP

B06  x=0?

B07  GTO A

B08  1

B09  STO+ F

B10  GTO B


A01  LBL A

A02  RCL F

A03  STOP

A04  R↓

A05  R↓

A06  STO A

A07  1

A08  -

A09  x≠0?

A10 GTO B

A11 RCL B

A12 FIX 4

A13 RTN



Labels used:  P, B, A

Variables:  F (factor), A, B





Casio fx-4000P Code: Prime Factorization



Instructions:


1.  Run program (P#) in RUN Mode (Mode 1).  

2.  Enter a positive integer at the "N?" prompt.  

3.  A prime factor is displayed.  Press [ EXE] to continue. Repeat step 3.

4.  The program ends when the integer reappears.  



"N" : ? → N : N → T : 2 → F : 

Lbl 3 : Frac( T ÷ F ) = 0 ⇒ Goto 2 :

F + 1 → F : Goto 3 :

Lbl 2 : F ◢ T ÷ F → T : T - 1 ≠ 0 ⇒ Goto 3 : N


Variables:  N, T, F  




Examples


26:  2, 13, 26   (26 = 2 × 13)


89:  89   (89 is prime)


175:  5, 5, 7, 175   (175 = 5^2 × 7)


1020:  2, 2, 3, 5, 17, 1020  (1020 = 2^2 × 3 × 5 × 17)




Eddie


All original content copyright, © 2011-2023.  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. 


Sunday, December 13, 2020

Numworks and DM42: Lucas-Lehmer Primality Test

 Numworks and DM42: Lucas-Lehmer Primality Test


Introduction


The Lucas-Lehmer Primality tests whether integers of the form 2^p - 1 are prime by repeating the loop p-2 times:


s_i+1 = s_i^2 - 2


with the initial value s_0 = 4.


At the end of the loop, if s = 0 mod (2^p - 1), then integer 2^p - 1 is a prime number.


Caution:  this test fails when p = 2.  The user will be asked to input an integer greater than 2.  


Numworks Python Script:  llt.py


from math import *

# Lucas-Lehmer Test

# 2020-12-06 EWS

p=float(input("p (>2)? "))

s=4

m=2**p-1

for i in range(p-2):

  # repeat p-2 times

  s=(s*s-2)%m

print("Mersenne Number:")

print(m)

if s==0:

  print("Prime")

else:

  print("Not Prime")


HP 42S/DM42 Program LLT


00 {66-Byte Prgm}

01 LBL "LLT"

02 "P (>2)?"

03  PROMPT

04  STO 01

05  ENTER

06  2

07  X<>Y

08  Y↑X

09  1

10  -

11 STO 04

12 R↓

13 2

14 -

15 STO 02

16 4

17 STO 03

18 LBL 00

19 RCL 03

20 X↑2

21 2

22 -

23 RCL 04

24 MOD

25 STO 03

26 DSE 02

27 GTO 00

28 RCL 04

29 "M= "

30 ARCL ST X

31 AVIEW

32 STOP

33 RCL 03

34 X=0?

35 "PRIME"

36 X=0?

37 AVIEW

38 END


Registers


R01: p

R02: p - 2

R03: s

R04:  m = 2^p - 1


Examples


p = 3

m = 7

Prime


p = 4 

m = 15

Not Prime


p = 5 

m = 31

Not Prime


p = 15

m = 32767

Not Prime


p = 20

m = 1048575

Not Prime


Source:


Cook, John D.  "Searching for Mersenne Primes" John D. Cook Consulting  November 28, 2018.  Retrieved December 5, 2020.  https://www.johndcook.com/blog/2018/11/28/searching-for-mersenne-primes/


"Lucas-Lehmer primality test"  Wikipedia.   Last edited June 29, 2020.  Retrieved December 5, 2020.

https://en.wikipedia.org/wiki/Lucas–Lehmer_primality_test


Eddie


Wednesday, March 22, 2017

HP 15C: Prime Factorization



HP 15C: Prime Factorization

This program does exactly this: factors an integer.  Fix 0 mode is activated during execution.  Each factor is displayed by pressing [R/S].  The calculator is returned to Fix 4 mode when the program is completed.  If the integer is a prime number, the program just returns the integer entered.  

Step
Key
Code
001
LBL B
42, 21, 22
002
FIX 0
42, 7, 0
003
STO 2
44, 2
004
STO 0
44, 0
005
2
2
006
STO 1
44, 1
007
LBL 3
42, 21, 3
008
RCL 0
45, 0
009
RCL÷ 1
45, 10, 1
010
ENTER
36
011
FRAC
42, 44
012
X=0
43, 20
013
GTO 2
22, 2
014
1
1
015
STO+ 1
44, 40, 1
016
GTO 3
22, 3
017
LBL 2
42, 21, 2
018
RCL 1
45, 1
019
R/S
31
020
R↓
33
021
R↓
33
022
STO 0
44, 0
023
1
1
024
-
30
025
X≠0
43, 30, 0  (TEST 0)
026
GTO 3
22, 3
027*
RCL 2
45, 2
028
FIX 4
42, 7, 4 
029
RTN
43, 32

Example:  150.  Factors:  2, 3, 5, 5 (when the display reads 150.0000 the factorization ends)
 
* Thanks to Ross Barnes to pointing the typo to me.  The key is now correct.  - Eddie 11/13/2021

This blog is property of Edward Shore, 2017.

Sunday, October 23, 2016

Fun with Primes: Sum of Primes

Fun with Primes:  Sum of Primes

Primes Composed of a Sum of Primes

Separated by 2

5 = 2 + 3
7 = 2 + 5
13 = 2 + 11
19 = 2 + 17
43 = 2 + 41
61 = 2 + 59
151 = 2 + 149
193 = 2 + 191

Other Sums

23 = 2 + 3 + 5 + 13
31 = 7 + 11 + 13
47 = 5 + 19 + 23
59 = 7 + 23 +29
71 = 3 + 31 +37
79 = 13 + 19 +47
79 = 5 + 31 +43
83 = 11 + 31+ 41
101 = 3 + 31 + 67
107 = 19 + 29 +59
109 = 3 + 47+ 59
113 = 11 + 41 +61
127 = 17 + 43 + 67
127 = 2 + 29 + 37 + 59
131 = 41 + 43 + 47
151 = 11 + 67 + 73
157 = 13 + 47 + 97
173 = 43 + 59 + 71
199 = 43 + 47 + 109
223 = 71 + 73 + 79
233 = 59 + 67 + 107
311 = 29 + 47 + 59 + 67 + 109

Primes Composed of a Sum and Product of Primes

31 = 2 * 5 + 7 * 3
43 = 17 + 2 * 13
47 = 2 * 7 + 3 * 11
61 = 2 * 11 + 3 *13
79 = 2 * 7 + 5 * 13
83 = 47 + 2 * (5 + 13)
89 = 7 + 2 * 41
149 = 2 * 17 + 5 * 23
149 = 2 * 59 + 31
163 = 2 + 7 * 23

What other combinations you find? 

Eddie

This blog is property of Edward Shore, 2016.



HP 15C: The I Squared Rule and HHC 2026

HP 15C: The I Squared Rule and HHC 2026 HHC 2026 Videos Before we start, the videos for HHC 2026 are up! Please take a look, ev...