Post Reply 
Massaging output of 50g's FACTORS command
01-03-2015, 07:12 AM
Post: #1
Massaging output of 50g's FACTORS command
For a value or expression, FACTORS returns a list of prime factors and their multiplicities: 1944 FACTORS returns {3 5. 2 3.}, for example.

How can I split that list into two, one {3 2} a list of the prime factors and the other {5. 3.} a list of their respective multiplicities? Some little RPL program will presumably do it.

This should be a few seconds' worth of fun for someone!
Find all posts by this user
Quote this message in a reply
01-03-2015, 03:08 PM (This post was last modified: 01-03-2015 03:09 PM by Gilles.)
Post: #2
RE: Massaging output of 50g's FACTORS command
Hi Peter, you can do for example :
Code:
 DUP
 1. « DUP TYPE NOT {DROP} IFT » DOSUBS
 SWAP
 1. « DUP TYPE {DROP} IFT » DOSUBS
Find all posts by this user
Quote this message in a reply
01-03-2015, 05:27 PM
Post: #3
RE: Massaging output of 50g's FACTORS command
Hi Gilles,

Thanks for that nice program. Is the leading "1." the type against which each member of the list is compared?

Peter
Find all posts by this user
Quote this message in a reply
01-03-2015, 05:32 PM (This post was last modified: 01-03-2015 05:38 PM by C.Ret.)
Post: #4
RE: Massaging output of 50g's FACTORS command
Hi, another way is to duplicate the list element by element into two distinct lists.
To separate factors from exponents, the process alternates the positions of the two lists during construction.
Code:
SPLIPT:
« { } DUP ROT 1      // Initiate exponent and factor lists and GETI sequence
  DO
     GETI            // Get i-th element
     5 ROLL +        // Roll down the exponent or the factors list & add the i-th ele.
     ROT ROT         // Roll back completed list in the "wrong" place (to alternate)
  UNTIL DUP 1 SAME   // loop until the entire inital list 
  END DROP2 »

The factors lsit { 3 5. 2 3.} will result in { 2 3 } and { 3. 5.} lists.
The list { y 5 p 1 p 0 a 2 H } will be split into the two lists 1:{ H a p p y } and 2:{ 2 0 15 }.
This last example is of course a season best greetings pretext.

So { y 5 p 1 p 0 a 2 H } SPLIT to everybody here !!
Find all posts by this user
Quote this message in a reply
01-03-2015, 06:16 PM
Post: #5
RE: Massaging output of 50g's FACTORS command
Hello C.Ret,

That's also nice. I had thought vaguely about GET, but not about GETI.

Peter
Find all posts by this user
Quote this message in a reply
01-03-2015, 09:16 PM (This post was last modified: 01-03-2015 09:21 PM by Gilles.)
Post: #6
RE: Massaging output of 50g's FACTORS command
(01-03-2015 05:27 PM)Peter Murphy Wrote:  Hi Gilles,

Thanks for that nice program. Is the leading "1." the type against which each member of the list is compared?

Peter

The leading "1." is the number of arguments taken from the list for the DOSUBS command. That means that the sequence « DUP TYPE NOT {DROP} IFT » DOSUBS takes elements in the list one by one and returns a list

with your example : { 3 5. 2 3.}

3 DUP TYPE NOT {DROP} IFT is executed, returns nothing (3 is TYPE 28.)
5. DUP TYPE NOT {DROP} IFT is executed, returns (5. is TYPE 0).
2 DUP TYPE NOT {DROP} IFT is executed, returns nothing
3. DUP TYPE NOT {DROP} IFT is executed, returns 3.

So DOSUBS -> returns { 5. 3. )

For example
{ "a" "b" "c" "d" } 3. « + + » DOSUBS will return { "abc" "bcd"}
that means :take the arguments from the list 3 by 3 and concatenate {"a "b" "c" + +, "b" "c" "d" + +}
Find all posts by this user
Quote this message in a reply
01-03-2015, 09:35 PM
Post: #7
RE: Massaging output of 50g's FACTORS command
Gilles,

Thanks for your kindly and detailed help. This Forum is an excellent place.

Peter
Find all posts by this user
Quote this message in a reply
01-04-2015, 09:58 AM (This post was last modified: 01-04-2015 10:23 AM by Gilles.)
Post: #8
RE: Massaging output of 50g's FACTORS command
I wondered if it was possible to split a list with only one DOSUBS and found this way :

Code:
1. « NSUB 1. == {{}{} ROT} IFT + SWAP » DOSUBS

{"a" "b" "c" "d" "e" "f" "g" "h" }
->
{ { "b" "d" "f" "h" } { "a" "c" "e" "g" } }

And to get the result in right order both with odd or even number of elements in the list

Code:

1. « NSUB 1. == {{}{} ROT} IFT + SWAP
     IF 'NSUB==ENDSUB AND NOT(NSUB MOD 2)' THEN SWAP END  » DOSUBS
Find all posts by this user
Quote this message in a reply
01-07-2015, 10:12 PM (This post was last modified: 01-07-2015 10:21 PM by Han.)
Post: #9
RE: Massaging output of 50g's FACTORS command
The code below basically changes the list into an array, then converts to a list of row vectors that are reverse-sorted, and then converted back to a matrix. Finally a transpose gets the desired form.
Code:
«
  OBJ→ 2. / { 2. } + →ARRY 
  →ROW →LIST REVLIST OBJ→ ROW→  @ delete this line of code if no sorting needed
  TRN →ROW DROP
  AXL SWAP AXL SWAP             @ delete this line of code if you do not have a preference for lists
»

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
01-08-2015, 11:11 AM
Post: #10
RE: Massaging output of 50g's FACTORS command
As we're talking about the HP 49G it's easy to write a programme that doesn't first make a list of factors & powers & then has to seperate them.

::
FPTR2 ^CK1Z
FPTR2 ^MSQFF
NULL{}
DUPROT
#2/
ZERO_DO
4ROLL
>TCOMP
SWAPROT
>TCOMP
SWAPLOOP
;

Oh for such flexibility on other platforms!
Find all posts by this user
Quote this message in a reply
Post Reply 




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