HP Forums
50g. Substitution of the FACTORS command - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: 50g. Substitution of the FACTORS command (/thread-9033.html)



50g. Substitution of the FACTORS command - pier4r - 09-10-2017 10:02 AM

I do not really like to use the built in command FACTORS when I would like to know the factors of a integer number in real format. FACTORS would require some flags to be set plus real to integer conversion.

Sure when I have massive needs, then FACTORS is better, but otherwise I'd like to have a more handy routine.

A quick idea is the one below, but do you know any one else that coded something more robust and fast? Or maybe a MathExt library containing a substitution of FACTORS?

Requirements:
input: an integer or an integer in real form (so with the dot at the end), flag -3 and -105 set or unset, does not matter.
output: the factors of the input number (integers in real or integer form)

Code:

factorsR
  \<<
    {} "resultList" DROP
    2 "possibleDivisor" DROP
    0 "divisorFound" DROP
    
    \->
    @input
    inputNumber
    
    @local var
    resultList
    possibleDivisor
    divisorFound
    \<<
      WHILE
        inputNumber ISPRIME? NOT
        inputNumber 1 >
        AND
      REPEAT
        
        2 'possibleDivisor' STO
        0 'divisorFound' STO
        WHILE
          divisorFound 0 ==
        REPEAT
          IF
            inputNumber possibleDivisor MOD 0 ==
          THEN
            1 'divisorFound' STO
            'resultList' possibleDivisor STO+
            inputNumber possibleDivisor / 'inputNumber' STO
          ELSE
            possibleDivisor NEXTPRIME 'possibleDivisor' STO
          END
        END
      END
      
      'resultList' inputNumber STO+
      
      resultList
    \>>
  \>>



RE: 50g. Substitution of the FACTORS command - Gilles59 - 09-10-2017 10:21 AM

(09-10-2017 10:02 AM)pier4r Wrote:  I do not really like to use the built in command FACTORS when I would like to know the factors of a integer number in real format. FACTORS would require some flags to be set plus real to integer conversion.

Hi Pier...
Another way :

Code:
My new Factors command :
«
 PUSH   @ save system flags
  DUP 
   RAD -3 CF  R->I  FACTORS 
  SWAP TYPE NOT { I->R } IFT
 POP @ Restaure system flags
»

If i'm not wrong it's OK with your requirements:
input: an integer or an integer in real form (so with the dot at the end), flag -3 and -105 set or unset, does not matter.
output: the factors of the input number (integers in real or integer form)
No change of the flags setup and angle mode