Tampilkan postingan dengan label lists. Tampilkan semua postingan
Tampilkan postingan dengan label lists. Tampilkan semua postingan

HP Prime: Reversing an Integer's Digits

HP Prime:  Reversing an Integer's Digits


(Inspired by the HHC 2022 programming contest)


What Should I Add To Reverse the Digits?


Let A, B, C, D, and E be individual digits (0-9) of an integer.   AB would represent a two digit integer with the value of 10 * A + B.  ABC would represent a three digit integer with the value of 100 * A + 10 * B + C.


Reversing a Two Digit Integer


AB + # = BA

10 * A + B + # = 10 * B + A

# = 9 * (B - A)


Example:  Let AB = 76.

A = 7, B = 6

# = 9 * (6 - 7) = -9

76 - 9 = 67


Reversing a Three Digit Integer


ABC + # = CBA

100 * A + 10* B + C + # = 100 * C + 10 * B + A

# = 99 * (C - A)


Example:  ABC = 469

# = 99 * (9 - 4) = 495

469 + 495 = 964


Reversing a Four Digit Integer


ABCD + # = DCBA

1000 * A + 100 * B + 10 * C + D + # = 1000 * D + 100 * C + 10 * B + A

# = 999 * (D - A) + 90 * (C - B)


Example:  ABCD = 7219

# = 999 * (9 - 7) + 90 * (1 - 2) = 1908

7219 + 1908 = 9127


Reversing a Five Digit Integer


ABCDE + # = EDBCA

10000 * A + 1000 * B + 100 * C + 10 * D + E + # =

10000 * E + 1000 * D + 100 * C + 10 * B + A 

# = 9999 * (E - A) + 990 * (D - B)


Example: ABCDE = 52693

# = 9999 * (3 - 5) + 990 * (9 - 2) = -13068

52693 - 13068 = 39625


Having the Calculator Do It


The program REVINT reverses the digits of an integer, up to 11 digits.   The program does not allow numbers that have non-zero fractional parts or integers more than 11 digits.  Instead of solving for # (see above), the program splits the integers into a list in reverse order, and uses list processing to get the final answer. 


HP Prime Program:  REVINT


Caution:  Integers that end or begin with zero may not return accurate results.   My suggestion is not use 0s with this program.  See examples below for more details.  


EXPORT REVINT(N)

BEGIN

// 2022-09-18 EWS

// reverse the integer N

// up to 12 digits

LOCAL D,P,A,I,M,L;

L:={};

P:=XPON(N);


// check size 

  IF P>11 THEN

  RETURN "TOO BIG";

  KILL;

  END;

 

// check type

  IF FP(N) THEN

  RETURN "NOT AN INTEGER";

  KILL;

  END;

   

D:=N;


// loop

  FOR I FROM P DOWNTO 0 DO

  A:=D/ALOG(I);

  L:=CONCAT({IP(A)},L);

  D:=D-IP(A)*ALOG(I); 

  END;

  

// rebuild 

M:=ΣLIST(MAKELIST(ALOG(X),X,P,0,−1)*L);

RETURN M; 

END;


Examples:


REVINT(4321) returns 1234


REVINT(56765) returns 56765   (56765 is a palindrome, reversing the digits results in the same number)


REVINT(42910) returns 1924 (01924 - be aware about integers ending or beginning with 0)


REVINT(67.28) returns "NOT AN INTEGER" (error)



Eddie


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


Drawing Random Numbers with Playing Cards 


An Easy Way to Get Random Numbers

Need an easy and fun way to draw random is to use a playing deck of cards.  No electricity or potentially expensive electronic devices are needed, and your shuffling skills will improve dramatically.  





Take a regular deck of cards, and you will only need 40 cards.  Get all the Aces, ones through nines, and one set of face cards.   Assign the following cards as the following values:


Aces:  1

Twos:  2

Threes: 3

Fours: 4

Fives: 5

Sixes: 6

Sevens: 7

Eights: 8

Nines: 9

Any set of face card or tens: 0  (I use Jacks, see the pictures above.  You can use Queens, Kings, or Tens)


To get a random number:

1.  Shuffle the deck

2.  Draw up to four cards 

3.  That card becomes the random number.  Insert a decimal point anywhere if appropriate.  


Why only up to four cards?  There are only four digits available per deck.  We are generating a samples with no replacement.  


Sample four digit numbers:

8-clubs, Ace-hearts, 2-clubs, 5-diamonds:  8125

2-diamonds, Ace-clubs, 3-clubs, 5-spades:  2135

4-hearts, 8-spades, 3-diamonds, 9-spades: 4839

4-hearts, 3-diamonds, 9-slides, 8-clubs: 4398

Jacks-clubs, Ace-clubs, 2-diamonds, 2-spades: 0122


How many possible permutations are possible with this deck?

2 digits:  nPr(40, 2) = 1560

3 digits:  nPr(40, 3) = 59,280

4 digits:  nPr(40, 4) = 2,193,360



Can we create a program to generate random numbers using this method?  

Yes.   I am using the TI-84 Plus CE to for this program.   The TI-84 has a randIntNoRep function, which generates a list of random integers without replacement.   The nice part of the is we are working with base 10, so the use of a remainder function will give the results we want: 0 through 9.  


Syntax:


Random Sample of Integers Without Replacement:

randIntNoRep(low, high, size of the sample)


Remainder function, which can be used as the modulus function.

remainder(x, y):  returns the remainder of x ÷ y


The program RANDDECK will generate a list and draw a histogram.


TI-84 Plus CE Program:  RANDDECK





"2022-08-16 EWS"

ClrHome

Disp "MAKE A 4 DIGIT,","NUMBER FROM A DECK 0-9"

Input "NO OF TRIALS: ", N

For(I,1,N)

randIntNoRep(1,40,4)→L6

remainder(L6,10)→L6

sum(L6*{1000,100,10,1})→N

If I=1

Then

{N}→L5

Else

augment(L5,{N})→L5

End

End

Pause L5

ClrHome

Disp "PRESS [ENTER] TO"

Pause "DISPLAY HISTOGRAM"

Plot1(Histogram,L5,1,BLUE)

PlotsOn 1

ZoomStat


Here is an example.



Until next time,


Eddie


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


Lists in Numworks - Version 19.2

Lists in Numworks - Version 19.2


Introduction


In Version 19,  lists were added as an object in the Numworks calculator.  We can define and name lists with any values that we want. Lists are designated by the brackets { }. 


*   The values can be real number, complex numbers, numbers with units, scientific constants, and combinations of those types.   What is not allowed in lists are strings and matrices.


*  The indexing of lists starts with 1.   We can call a list's elements by the parenthesis after the list name.  For example,  xlist(10) recalls the 10th element of the list xlist.


*  { f(k) }_k≤value generates a list of f(k) from k=1 to value, step 1.   The variable can be almost any variable you want, except e and i.  e is designated as the exponential constant (about 2.71828...) and i is designated as the imaginary number √-1.   The limits are strictly from 1 to value.


*  Once a user defined list is created, the individual values cannot be changed.   Furthermore, there are no augment or delete commands.  In this sense, user defined lists acts like tuples in Python.


*  Lists can be recalled in the Statistics and Regression apps.   The system named lists V#, X#, and Y# are updated accordingly.


* Defining a list of random values will always change the randomized values every time a user defined list is recalled.  


Please note that this is for Version 19.2.  


Screenshots are captured using the Numworks Emulator on July 10, 2022:  www.numworks.com/emulator
















Note:  Casio fx-991EX Week - September 5, 2022 to September 9, 2022 


Eddie


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



TI-84 Plus CE Python: The Split Command and Some Statistics

TI-84 Plus CE Python:  The Split Command and Some Statistics


Python:  Split Command


The split command allows the us to accept a list of input, use a character to split (spaces, commas, semicolons, etc.).


Example:


list1=input("List: ").split(",")   # split by comma

print(list1)


List:  1, 3, 4, 5

[ '1', '3', '4', '5' ]


Remember that the default of the input command is strings, so we have to change data types when needed:


n=len(list1)

for i in range(n):

  list1[i]=float(list[i])


(this is one way to do it)


The .split command does not require a module.  



TI-84 Plus CE Python Program:  SIMPSTAT


# Math Calculations

from math import *

print("Simple Statistics")

print("Edward Shore")

# 2022-04-27


x=input("\nSeparate data points \nby a comma: ").split(",")


# make every entry float

n=len(x)

for i in range(n):

   x[i]=float(x[i])


# \t tab \n new line

print("\nn =\t",n)


# sum

s=sum(x)

print("sum =\t",s)


# mean

a=s/n 

print("avg =\t",a)


# population deviation

d=0

for i in range(n):

  d+=(x[i]-a)**2

d=sqrt(d/n)

print("det =\",d)


# range of data

m0=min(x)

m1=min(x)

m2=m1-m0

print("min =\t",m0)

print("max =\t",m1)

print("range\t",m2)



Example


Sample list:  5.8, 5.9, 6.7, 6.9, 8.2, 8.8, 9.1

(use a comma to separate list items)


Results:

n = 7

sum = 51.4 (displayed as 51.400000000004)

avg = 7.342857142857143

dev = 1.25454276572008

min = 5.8

max = 9.1

range = 3.3


Source


Rohit.  "How to take list input in Python in single line"  Tutorial By EyeHunts.   December 11, 2021.  https://tutorial.eyehunts.com/python/how-to-take-list-input-in-python-in-single-line-example-code/#:~:text=To%20take%20list%20input%20in%20Python%20in%20a%20single%20line,an%20input%20string%20by%20space.

Last Retrieved April 28, 2022.  


Hope you find this useful.  Until next time,


Eddie 


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


Backlink 9999 Traffic Super

Order Now...!!!!