Showing posts with label modulo. Show all posts
Showing posts with label modulo. Show all posts

Saturday, May 8, 2021

Some PPC Journal Programs Ported to HP 12C, TI-84 Plus CE Gets Python in the United States

Some PPC Journal Programs Ported to HP 12C, TI-84 Plus CE Gets Python in the United States


Introduction


In 1978, the Personal Programmers Club wrote an issue containing short utility programs for the classic HP 25A and HP 67 calculators.  On today's blog, I am going to port some of these routines for the HP 12C financial calculator.   The calculator codes come from the standard-RPN HP 12C (99 step capacity).  For those using a HP 12C Platinum, some key codes will differ (example: LST X).


See the Source section for more details.


HP 12C Program:  Modulo Division (remainder)


Original HP 25A and HP 67 (section C1) program by John Kennedy.


This program uses register 1, but any register can be used.  This program works best when x and y are both positive.



01 x<>y 34

02 STO 1 44, 1

03 x<>y 34

04 STO÷ 1         44, 10, 1

05 RCL 1 45, 1

06 INTG 43, 25

07 ×         20

08 -         30

09 GTO 00         43, 33, 00


If you want the result of the division with both the integer and remainder part, press RCL 1.


Example:


4758 mod 360 = 78


4758, ENTER, 360, R/S


HP 12C Program:  Infinite Division


Original HP 25A and HP 67 (section C2) program by John Kennedy.  


This program allows you to see each digit of a division y/x, one digit at a time.  It does not tell you where the decimal point is, you will need to determine that on your own.  


The original code for the HP 25A took the advantage of the storage registers R3 and R7.  In statistics, R3 = n and R7 = ∑x allowing for the mean function to calculate the division.  It was assumed that two registers are cleared before program execution.  


The original code also pause for each digit.  The HP 12C does not have a pause command, so I used the run/stop (R/S) command instead.  


01 STO 1 44, 1

02 x<>y 34

03 STO 2 44, 2  // main loop begins here

04 RCL 1 45, 1

05 STO÷ 2         44, 10, 2

06 RCL 2 45, 2

07 INTG 43, 25

08 R/S         31 // display the digit

09 ×         20

10 -         30

11 1         1

12 0         0

13 ×         20

14 GTO 03         43, 33, 03


Example:  Find the decimal equivalent of 45/78.


45, ENTER, 78, R/S   (R/S for each digit)


Results: 0, 7, 8, 9, 4, 7 (and so on)


45/78 = 0.78947368421052631...


HP 12C: Reverse of Integer's Digits


Original HP 25A and HP 67 (section D7) program by Jim Davidson


This program reverses the digits of an integer.   Example: the program transforms 82531 to 13258.


01 STO 0 44, 0

02 STO- 0         44, 30, 0

03 FRAC 43, 24

04 STO+ 0         44, 40, 0

05 LST x 43, 36

06 INTG 43, 25

07 .         48

08 1         1

09 STO÷ 0         44, 10, 0

10 ×         20

11 x=0         43, 35

12 GTO 14         43, 33, 14

13 GTO 03         43, 33, 03

14 RCL 0 45, 0

15 GTO 00         43, 33, 00


Example:  Reverse the digits of 90649.


90649, R/S


Result:  94609



Source


Personal Programmers Club. (various authors)   "Special Routines Issue"  Vol. 5 No. 7 August 1978  Publication: Santa Ana, CA 


TI-84 Plus CE Gets Python (in the U.S.)

TI-84 Plus CE Python Comes to the United States this fall.  Texas Instruments already has two Python powered calculators in the TI-84 family in France:

*  TI-83 Premium CE Edition Python (current - rechargeable battery)
*  TI-82 Advanced Edition Python (to come in Fall 2021 - powered by AAA batteries)

The TI-84 Plus CE Python will have a rechargeable battery.  More information is found here:  https://education.ti.com/en/products/calculators/graphing-calculators/ti-84-plus-ce-python


Eddie


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

HP Prime and Numworks: Luhn Algorithm

 HP Prime and Numworks: Luhn Algorithm


The Luhn Algorithm:   Is Your Credit Card Number Valid?


The Luhn algorithm, created by IBM scientist Hans Peter Luhn in 1954, also known as the modulus 10 algorithm, is a checksum algorithm used to check the validity certain numbers used in financial transactions, such as identification numbers and credit card numbers. The algorithm is in public domain and serves as a basic check.  


The general steps of the Luhn Algorithm:


1.  Starting at the rightmost digit, double every other digit (2nd digit from the rightmost digit, 4th digit from right, 6th digit from right, and so on)


2.  If the digit that is doubled is at least 10, add the digits together.  


Replace 10 with 1.

Replace 12 with 3.

Replace 14 with 5.

Replace 16 with 7.

Replace 18 with 9.


3.  Take the sum of all the digits.


4.  If the sum is a multiple of 10 (sum modulo 10 is equal to zero), the number is considered valid.


Example:  4366357


For illustration purposes, let's separate each of the digits:


4  3  6  6  3  5  7


There are 7 digits.  


2nd digit from the most right:  5.  Double it to 10.  1 + 0 = 1


4  3  6  6  3  1  7


4th digit from the most right:  6.  Double it to 12.  1 + 2 = 3


4  3  6  3  3  1  7


6th digit from the most right:  3.  Double it to 6.  


6  6  3  3  1  7


Now add the digits:  4 + 6 + 6 + 3 + 3 + 1 + 7 = 30


10 divides into 30 evenly, or 30 mod 10 = 0.   The integer 4366357 is valid according to the Luhn algorithm.  


HP Prime Program:  LUNH


EXPORT LUHN()

BEGIN

// Luhn Algorithm - HP Prime

// strings are used to accommodate

// string: type 2

LOCAL s,l,k,c,t,n;


INPUT({{s,[[2]]}},

"Luhn Algorithm",

{"N?"},{"Integer"});

l:=DIM(s);

k:=l;


// total

t:=0;


// loop

WHILE k>0 DO

n:=EXPR(MID(s,k,1));

IF FP((l-k)/2)==0.5 THEN

IF n≠0 THEN

n:=(2*n-1) MOD 9+1;

END;

END;

t:=t+n;

k:=k-1;

END;


// final results

IF t MOD 10==0 THEN

RETURN "VALID";

ELSE

RETURN "NOT VALID";

END;


END;


Numworks Python Script:  luhn.py


from math import *

# Luhn Algorithm

# 2021-01-10 EWS

# Numworks Python


# strings are used

s=str(input('Integer? '))

l=len(s)

k=l


# total

t=0


# loop

while k>0:

  n=float(s[k-1])

  f=(l-k)/2

  if f-int(f)==0.5:

    if n!=0:

      n=fmod(2*n-1,9)+1

  t=t+n

  k=k-1


# final results

if fmod(t,10)==0:

  print("VALID")

else:

  print("NOT VALID")



Examples


38348 returns NOT VALID


14956 returns NOT VALID


4382 returns VALID



Source


"Luhn algorithm" GeeksforGeeks. Noida, Uttar Pradesh.  https://www.geeksforgeeks.org/luhn-algorithm/  Retrieved January 1, 2021.


"Luhn algorithm".  Wikipedia.  Last Updated December 23, 2020.  https://en.wikipedia.org/wiki/Luhn_algorithm  Retrieved January 1, 2021.  



Eddie


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


Thursday, November 28, 2019

TI 84 Plus CE and HP Prime: Simplified Modulo Expressions

TI 84 Plus CE and HP Prime: Simplified Modulo Expressions

I hope you are having a great, at least sane Thanksgiving. It's Turkey Day (or the vegan equivalent of turkey day) in the United States.  Disclosure: I'm not vegan. 



Introduction

Let A, B, and M be positive integers where:

A ≡ B mod M

Let A = a * c   (A = a when c = 1)
B = b * c  (B = b when c = 1)
M = m

A "cancellation" theorem states that if:

a * c ≡ b * c mod m and gcd(c,m) = 1, then

a ≡ b mod c

Also, if a * c ≡ b * c mod m with gcd(c,m) = d, then

a ≡ b mod (m/d)

The program SIMPMOD uses the second theorem to find equivalent congruence for A ≡ B mod M.   The user inputs B and M, A and equivalent congruence will be calculated.

TI-84 Plus CE Program: SIMPMOD

Firmware version 5.3 or greater is required because of the toString command.  For earlier versions, you will have edit the last few lines. 

"2019-10-27 EWS"
"84+ CE 5.3"
ClrHome
Disp "A =  B MOD M"
Disp "A>0, B>0, M>0"
Input "B? ",B
Input "M? ",M
remainder(B,M)→A
Disp "A= ",A
iPart(√(B))→S
{1,B}→L_1
For(K,2,S)
B/K→T
If fPart(T)=0
augment(L_1,{K,T})→L_1
End
dim(L_1)→D
For(K,1,D)
L_1(K)→C
If fPart(A/C)=0
Then
A/C→R
B/C→S
M/gcm(M,C)→T
toString(R)+" = "+toString(S)+" MOD "+toString(T)→Str1
Disp Str1
End
End 
Disp "DONE"

L_1 is the L1 list variable, [2nd] [ 1 ]

HP Prime Program: SIMPMOD

CHAR(8801) or CHAR(#2261h) is the congruence symbol, ≡

EXPORT SIMPMOD()
BEGIN
// 2019-10-27 EWS
// A ≡ B MOD M
LOCAL B,M;
LOCAL A,R,S,T,K,l1,l2,D;
INPUT({B,M},
"A "+CHAR(8801)+" B MOD M",
{"B?","M?"});
A:=B MOD M;
PRINT();
l1:=CAS.idivis(B);
l2:=SIZE(l1);
D:=l2(1);

FOR K FROM 1 TO D DO
C:=l1(K);
IF FP(A/C)==0 THEN
R:=A/C;
S:=B/C;
T:=M/CAS.gcd(M,C);
PRINT(R+" "+CHAR(8801)+" "+
S+" MOD "+T);
END;
END;
PRINT("DONE");
END;

Examples

Example 1

20 ≡ 500 MOD 30

Inputs:  B = 500, M = 30

20 ≡ 500 MOD 30
10 ≡ 250 MOD 15
5 ≡ 125 MOD 15
4 ≡ 100 MOD 6
2 ≡ 50 MOD 3
1 ≡ 25 MOD 3

Example 2

4 ≡ 364 MOD 60

Input: B = 364, M = 60

4 ≡ 364 MOD 60
2 ≡ 182 MOD 30
1 ≡ 91 MOD 15

Example 3

28 ≡ 3528 MOD 100

Input:  B = 3528, M = 100

28 ≡ 3528 MOD 100
14 ≡ 1764 MOD 50
7 ≡ 882 MOD 25
4 ≡ 504 MOD 100
2 ≡ 252 MOD 50
1 ≡ 126 MOD 25

Source:

Dudley, Underwood.  Elementary Number Theory  2nd Edition.  Dover Publications, Inc:  Mineola, NY 1978  ISBN 978-0-486-46931-7  (2008 reprint)

Happy Thanksgiving!

Eddie

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

HP 20S: Acoustics Programs

HP 20S: Acoustics Programs Program A: Speed of Sound in Dry Air cs = 20.05 × √(273.15 + T°C) Code: 01: 61, 41, A: LBL A 02: ...