Showing posts with label permutations. Show all posts
Showing posts with label permutations. Show all posts

Saturday, June 19, 2021

7000G Retro Month - June 19 Edition

7000G Retro Month - June 19 Edition





Introduction


Welcome to the 7000G Retro Month, which features programming for the classic Casio calculators from the mid/late 1980s:  primarily fx-7000G and fx-7500G.  Since the programming language stays similar throughout the years, programs can be translated to the fx-6300G and later graphing calculators with little to no adjustments.  Non graphic programs should be ported to the fx-4000P, fx-4500P (A), fx-3650p (II), fx-50F Plus (II), and fx-5800P with little to no adjustments.  


7000G Retro Month takes place every Saturday during June 2021.


To make text easier to type, I can going to use the following text friendly symbols for the following:


->  for →


/I for ⊿


=> for ⇒


What do you think?   Unicode or simple text equivalents?  


- - - - - - -- - -- - -


Today's subject revolves around Probability and Random Numbers.  Enjoy!


- - - -- - - -- - -- -- -


Random Integers: Repeats Allowed 


This program allows the user to generate a number of integers between A and B, repeats are allowed.   Each integer is displayed one at a time.


"A"? -> A

"B"? -> B

"N"? -> N

Lbl 1

Int ((B-A+1) Rnd#) /I

Dsz N

Goto 1


Combinatorics


The program allows the user to choose between three options:


1.  PERM:  permutation:  nPr

2.  COMB:  combination:  nCr

3.  COMB REPLACE:  combination with replacements allowed (n+r-1)Cr


"N"? -> N

"R"? -> R

"1. PERM"

"2. COMB"

"3. COMB REPLACE"

? -> K

K=1 => N!÷(N-R)! -> X

K=2 => N!÷(R!(N-R)!) -> X

K=3 => (N+R-1)!÷(R!(N-1)!) -> X

X


Binomial Distribution


This program calculates the sum of probabilities for a binomial distribution:


total probability = ∑( nCr(N,K) p^K (1-p)^(N-K), K=A to B)


Probability:  0 < p < 1


"A"? -> A : "B"? -> B

"N"? -> N : "P"? -> P

0 -> M : Lbl 1

M+(N!×P^A×(1-P)^(N-A))÷(A!(N-A)!) -> M

A+1 -> A

A>B => Goto 2

Goto 1

Lbl 2

M


Confidence Interval


This program generates a confidence interval using one of four probabilities are assigned to the following variables:


F: 99%  (z* ≈ 2.576)

G: 98% (z* ≈ 2.326)

H: 95% (z* ≈ 1.96)

I: 90% (z* ≈ 1.645)


interval = mean ± z* × variance / √n


"CONF INTERVAL"

2.576 -> F

2.326 -> G

1.96 -> H

1.645 -> I

"MEAN"? -> A

"VAR"? -> B

"N"? -> N

"F=.99, G=.98"

"H=.95, I=.90"

? -> J

A-JB÷√N -> E /I

A+JB÷√N -> F


E:  low, F: high


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, October 8, 2017

Adventures in Python: Combinations and Permutations

Adventures in Python: Combinations and Permutations

This program offers the user a choice of one of three calculations:

1.  Combinations, no repeats
2.  Permutations
3.  Combinations, with repeats

The program demonstrates how a menu is made.  Equality is tested with two equal signs (==).  A single equal sign (=) represents assignment for the Python.

The math.factorial function uses integers.

#  Program 005: Combinations, Permutations, Repeated Combination
#  Demonstration of the If structure

import math
# factorial is only for integers

# choices
# we will need to format the choice variable as an integer (number)
print("Make your choice:")
print("1.  Combination")
print("2.  Permutation")
print("3.  Repeated Combination")
choice = int(input("Choice: "))

# error condition, also input routine
if choice < 1 or choice > 3:
    print("Not a valid choice")
else:
    n = int(input("n = "))
    r = int(input("r = "))
    nf = math.factorial(n)
    rf = math.factorial(r)
    df = math.factorial(n-r)

# calculation test.  Equality is tested using 2 equal signs
if choice == 1:
    calc = nf/(df*rf)
    print("Combinations = ",calc)

if choice == 2:
    calc = nf/df
    print("Permutations = ",calc)

if choice == 3:
    calc = math.factorial(n+r-1)/(rf*math.factorial(n-1))
    print("Combinations = ",calc)

Example: n = 52, r = 5

Make your choice:
1.  Combination
2.  Permutation
3.  Repeated Combination
Choice: 1
n = 52
r = 5
Combinations =  2598960.0
>>>
Make your choice:
1.  Combination
2.  Permutation
3.  Repeated Combination
Choice: 2
n = 52
r = 5
Permutations =  311875200.0
>>>
Make your choice:
1.  Combination
2.  Permutation
3.  Repeated Combination
Choice: 3
n = 52
r = 5
Combinations =  3819816.0

Eddie


This blog is property of Edward Shore, 2017.

Monday, January 25, 2016

Thursday, July 30, 2015

Generating Permutations: TI-84 Plus, Casio Prizm, and HP Prime (updated 10/18/15)

Generating Permutations:  TI-84 Plus, Casio Prizm, and HP Prime

In combinatorics, a permutation is an ordered arrangement of numbers, typically 1 to n.  For n = 3, there are 6 (3!) possible permutations:  {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, and {3,2,1}.

This blog will focus on generating a random permutation given n elements.

TI-84 Plus:  Using the randIntNoRep function.

For the TI-84 Plus, generating a permutation is rather simple with the randIntNoRep (random integers no reputation function).  The syntax to be used is:

randIntNoRep(1,n,n)

For n = 3, the syntax would be randIntNoRep(1,3,3).  You can find this function by pressing [math], PROB submenu, selecting option 8.


For the Casio Prizm and the HP Prime, a program will be needed.  I gave the program the title SAM for sample (no repeating numbers).  Generally, SAM uses two lists.  The first list is generated by a sequence from 1 to n.  The second is rearranged list.  In order to get sample to not have repeats, during the main loop, as numbers are picked, they are replaced by 0.  During subsequent picks from the first list, the loop tests to see for a nonzero list.  The second list is the output.

Note that with the Casio Prizm that lists must have at least one element to be initialized, where the HP Prime allows for blank lists (lists with zero elements). 

Casio Prizm:  SAM

“N”?→N
Seq(x,x,1,N,1)→List 1
RanInt#(1,N)→C
List 1[C]→M
{M}→List 2
0→List 1[C]
1→I
Do
RanInt#(1,N)→C
List 1[C]→M
If M≠0
Then
Augment(List 2,{M})→List 2
0→List 1[C]
1+I→I
IfEnd
LpWhile I≠N
List 2


HP Prime: SAM

Input:  SAM(n)

EXPORT SAM(n)
BEGIN
LOCAL s,L0,L1,I,c;

L0≔MAKELIST(X,X,1,n,1);
L1≔{ };
I≔0;

REPEAT
c≔RANDINT(1,n);
IF L0(c)≠0 THEN
L1≔CONCAT(L1,{L0(c)});
L0(c)≔0;
I≔I+1;
END;
UNTIL I==n;

RETURN L1;

END;


Update:  Changing c:=RANDINT(n) to c:=RANDINT(1,n) forces the random integer function to choose a number between 1 and 0, since RANDINT(n) allows for zero, allowing for a number to repeat, which is what we don't want.   Eddie

Note:  This is different from the permutation function (nPr, PERM) on your calculator, which calculates the number of arrangements of r from n objects.  (Formula: n!/(n-r)!)
  
Eddie


This blog is property of Edward Shore, 2015.

Wednesday, October 24, 2012

Permutations: When Objects Repeat

Monday's blog entry (10/22/2012), Factorials and Arrangements of Unique Objects dealt with permutations of arranging a group of objects where all the objects are unique. Today's blog entry looks at three situations where objects can be repeated.

All the Choices are Available all the Time

This is where you make permutations in which all the objects are available for each slot. For example, let's take a five digit zip code. There are five slots and for each slot the 10 digits are available: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

For the first slot there are 10 choices. For each of the 10 choices, the second slot presents another 10 choices. For each of those 10 choices, the third slot presents another 10 choices. And so on.

The number of arrangements is: 10 × 10 × 10 × 10 × 10 = 100,000. There are 100,000 five-digit zip codes possible, 00000 to 99999.

Remember when calculating permutations, order matters. For our example, 11110, 11101, 11011, 10111, and 01111 are five different arrangements. The general rule is presented below.


Permutations where all the choices are available all the time = n^k

where
n = number of objects
k = number of slots


Objects Can Repeat, but with Some Restrictions

This is similar to the first situation in the way of calculating the number of permutations (arrangements).

Let's say we are on a very famous game show. One of it's mini games is played for a car valued anywhere from $20,000 to $59,999. We want to know how many possible prices for this game are possible.

Looking at the range from $20,000 to $59,999: we can see the choices for each of the five digits. The first digit must be a 2, 3, 4, or 5. The other four digits can be anything 0 through 9 (10 choices each).

The number of prices possible are 4 × 10 × 10 × 10 × 10 = 40,000.

So using a random guess, the contestant has a 1 in 40,000 choice in getting the price exactly right.

Another mini-game offers cars from $11,111 to $36,666 where the first digit is given to the contestant (1, 2, or 3) and the contestant tries to roll the other four digits using a single die. The die contains the numbers 1, 2, 3, 4, 5, and 6. Our task to find out how many prices are possible.

There are 3 choices for the first digit, and 6 choices for the other four digits.

The number of prices possible are 3 × 6 × 6 × 6 × 6 = 3,888.

This mini-game can be played up to 3,888 times before a price repeats.


Permutations where:
1) Different slots can have restrictions
2) Each choice is independent

P = (number of choices for slot 1) × (number of choices for slot 2) × ... × (number of choices for slot k)


Rearranging a Group of Objects where some of the Objects Repeat

In this situation we are finding the number of permutations of a group of objects, except some of the objects repeat.

Let's try to find the number of ways to arrange the letters in the word PHYSICS, removing any restriction that the arrangement has to make a sensible word (HYSSICP would count as arrangement).

In the word PHYSICS, there is 1 "P", 1 "H", 1 "Y", 2 "S"s, 1 "I", and 1 "C", for a total of 7 letters. PHYSICS counts as one permutation, regardless which "S" is used in each slot. We still have 7 letters, which can be arranged 7! = 5,040 ways, but have to account for the 2 "S"s.

The true number of ways to arrange the letters in the word PHYSICS is 2,520 ways.

7! / 2! = 5,040/2 = 2,520

Coincidentally, the calculation is really 7! / (1! × 1! × 1! × 2! × 1! × 1!). However, 1! = 1. Hence, 1! × 1! × 1! × 2! × 1! × 1! = 2!.

Let's take another example. Find the number of ways to arrange the letters in the "word" AAABB. In this example, there are 3 "A"s and 2 "B"s for a total of 5 letters.

If all the letters were unique, the number of ways is 5!. But we have to account for the repeats. Divide by 3! for the "A"s and 2! for the "B"s. The result is:

5! / (3! × 2!) = 120 / (6 × 2) = 10

There are only 10 ways to arrange the letters of the "word" AAABB. The ten are:

AAABB
AABAB
AABBA
ABABA
ABBAA

BABAA
BBAAA
ABBAA
AABBA
BAAAB


Finding the Number of Arrangements of Objects Where Some Objects Repeat

n! / ( (k_1)! × (k_2)! × ... × (k_m)! )

where:
n = the total number of objects, including repeated objects
m = the number of unique objects
k_1 = number of "k_1" objects
k_2 = number of "k_2" objects
and so on until...
k_m = number of "k_m" objects

The expression above is known as a multinomial coefficient.


I hope this day is well for each of you. Thank you for your comments and suggestions. Take care,

Eddie


Source:
Marcus, Daniel A. "Combinatorics: A Problem Orientated Approach" Mathematical Association of America, Washington DC. 1998.


This blog is property of Edward Shore, 2012.

Tuesday, October 23, 2012

Factorials and Arrangements of Unique Objects

Last weekend my dad asked me about my blog, and the last entry was about factorials of large numbers. Clicking on this link will take you there. On our way to classic car auto shop in Orange, CA; my dad and I talked about the factorials as I tried to come up with a way of finding applications using factorials. Last weekend became the inspiration for my blog entry. Love you, Dad.

Note: This blog entry will cover factorials of non-zero integers, that is n = 0, 1, 2, 3...


Factorials

The factorial of a non-negative integer, written with an exclamation mark after the number, is defined as:

n! = n × (n - 1) × (n - 2) × (n - 3) × ... × 3 × 2 × 1

Start by multiplying n by n less 1, then multiplying the product by n less 2, and repeat until you get to 1.

Examples
5! = 5 × 4 × 3 × 2 × 1 = 120
8! = 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 40,320


What about 0! ?

By definition, 0! = 1. Dr. James Tanton, Ph. D (www.jamestaton.com) gives an explanation why mathematicians choose to in video: Link to Video

Simply put, 0! = 1 is just defined this way.


Arrangements

In this section, we consider three common arrangement problems. The task is to find the number of possible permutations a group of objects can be arranged. A permutation is an arrangement where the order of which the objects are placed is important.

In this section, we are going to arrange all the objects.

Arrangement of Unique Objects

Consider a bookshelf that has room for five books. For simplicity, let's call the books A, B, C, D, and E (and not "Combinatorics", "The Irrationals", "Euclid's Number", "Programming", and "Making Soup for Dummies" like I originally planned.)

The for the first slot there are five choices. The second slot provides four choices. Whatever is available for the second slot depends on what book was put in the first slot. For example, if I choose to put book A in the first slot, the books B, C, D, and E are available for the second slot. Instead, if I choose to put book C in the first slot, then A, B, D, and E are available.

For each choice I make on the first slot, I get four choices for the second. Considering the first two slots alone, this gives me a total of 5 × 4 = 20 arrangements.

Continuing in this way, there are three choices for the third slot, two choices for the fourth slot, and whatever is left over gets the fifth slot.

So, the total number of arrangements of five books is:

5 × 4 × 3 × 2 × 1 = 5! = 120

Yes, 120 different arrangements. Since order is important, the arrangement is considered a permutation.

In general, working with n objects and n slots:
There are n objects for the first slot,
there are n - 1 objects for the second slot,
there are n - 2 objects for the third slot,
and so on,
until we reach 1 slot left for the last object.

Hence, the number of arrangements are:

n × (n - 1) × (n - 2) × ... × 1 = n!


Number of Permutations of n Unique Objects = n!


Arrangement of Unique Slots, Limited Spaces Available

Let's go back to our problem of arranging five books (A, B, C, D, and E) but this time, we only have three slots available.

For the first slot, I have 5 books available to choose from. Depending on what I choose, I will have 4 books for the second slot. Each of those 4 books present a choice of the 3 books for the last slot. Whatever is left either goes somewhere else in the house or gets donated.

The number of arrangements that are available to me has decreased due to the fact I only have three slots available. Hence, 5 × 4 × 3 = 60.

Observe if we multiply 5 × 4 × 3 by (2 × 1)/(2 × 1) we get:

5 × 4 × 3
= (5 × 4 × 3 × 2 × 1) / (2 × 1)
= 5! / (2 × 1)
= 5! / (5 - 3)!

Basically we are taking all of the ways 5 books can be arranged, and then dividing that number by all the ways the 2 books that are not going to be used can be arranged.

Doing the above in the general case is how we arrive at the formula for permutations:

nPk = n! / (n - k)!

where we have n objects and k slots to fill.


Permutations: nPk = n! / (n - k)! where n ≥ k. Order is important.


Until next time, have a great day!

Eddie



This blog is property of Edward Shore, 2012.

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...