Post Reply 
(11C) n-th term of a Geometric Sequence
05-21-2018, 12:47 PM (This post was last modified: 05-21-2018 01:31 PM by Dieter.)
Post: #2
RE: (11C) n-th term of a Geometric Sequence
(05-21-2018 09:31 AM)Gamo Wrote:  Formula: a sub n = ar^n-1 for every integer n ≥ 1

an = a1 · rn–1

(05-21-2018 09:31 AM)Gamo Wrote:  
Code:
LBL D
RCL 2
X>0?   // Test Common Ratio for Positive or Negative value 
GTO 1
...
X=0?    // Test n-th term for Even or Odd number
GTO 3
GSB 2    // Odd integer of n-th term
RCL 1
x
RTN
LBL 1    // Common Ratio is Greater than Zero
...
LBL 2    // identical routine for common ratio value Less than Zero
...
LBL 3    // Even integer of n-th term
GSB 2
CHS
RCL 1
x
RTN

Gamo, all these tests and subroutines are not required. You do not have to check whether r is negative or not, and if it is, also check if n is odd or even. All HP calculators I know can calculate integer powers of negative bases directly. So (–3,14)5 directly yields –305,24... without any problem.

This means the more than 40 steps routine at LBL D can be replaced with this:

Code:
LBL D
RCL 2
RCL 3
1
-
Y^X
RCL 1
x
RTN

That's it.

If you really want to check for odd or even n and whether r is negative or not, you could do it much shorter. Simply calculate the power with |r| and change the sign if r<0 and n is even. For instance this way:

Code:
LBL D
CF 0
RCL 2
X>0?
GTO 1  // keep flag 0 clear for r>0
RCL 3
2
/
FRAC   // for r<0 check if n is odd or even
X=0?   // if n is even
SF 0   // set flag 0
LBL 1
RCL 2
ABS    // calculate |r|^(n-1)
RCL 3
1
-
Y^X
RCL 1
x
F? 0
CHS    // change sign if r<0 and n even
CF 0
RTN

But again: all this is not required.

You know I'm a big fan of using the stack instead of registers, so here's how I would do it:

Code:
LBL A
ENTER
1
-
X<>Y
ENTER
R↓
X<>Y
Y^X
x
RTN

The ENTER in the second line usually is not required, but this way the program will also run on an HP25. ;-)

Usage:
a1 [ENTER] r [ENTER] n
[A] => an
[x] => an+1
[x] => an+2
...

Example:
1 [ENTER] –3 [ENTER] 1
[A]   1
[x] –3
[x]   9
[x] –27
[x]   81
[x] –243
...

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (11C) n-th term of a Geometric Sequence - Dieter - 05-21-2018 12:47 PM



User(s) browsing this thread: 1 Guest(s)