ALLF by Joseph K. Horn -- An HP 48 Number Theory tool Calculates ALL the factors (prime and composite) of x Dedicated to Dr. Robert Wilson +-------------------------------------------------------------------------+ | NOTE: This program calls FACTOR, a program by Jurjen NE Bos, available | | on EduCALC Goodies Disk #2. You must be in the FACTOR directory when | | you upload ALLF, or it will not work. | +-------------------------------------------------------------------------+ Finding the factors of a number can be done by brute search for small numbers, but that takes too long for large numbers. The following program is optimized for speed with large inputs. For example, it takes an input of 842632 and only 1.5 seconds later it returns all 36 factors (viz. 1 2 4 7 8 14 28 41 56 82 164 287 328 367 574 734 1148 1468 2296 2569 2936 5138 10276 15047 20552 30094 60188 105329 120376 210658 421316 842632). Is there a faster way? INSTRUCTIONS: Place a real or binary integer in level 1 and press ALLF. The result will be a list of its factors tagged with their count. For example, type 28 and press ALLF and see 6: { 1 2 4 7 14 28 } which means that 28 has 6 factors, followed by the list of them. %%HP:T(3); @ ALLF by Joseph K. Horn @ Finds ALL the factors (prime & composite divisors) of x. @ Uses FACTOR by Jurjen NE Bos. @ Input: positive integer. Output: n:{f1,f2,f3,...,fn}. \<< FACTOR OBJ\-> IF DUP THEN { 1 } [ 1 ] ROT 2 + 3 1 \-> p \<< FOR n n ROLL IF DUP p \=/ THEN SWAP DROP OVER OBJ\-> \->ARRY SWAP DUP 'p' STO END * DUP OBJ\-> EVAL \->LIST ROT SWAP + SWAP -1 STEP \>> DROP OBJ\-> EVAL DUP DUP 2 + ROLLD \->LIST SWAP \->TAG ELSE DROP :1: { 1 } END \>> ---------- Note: The factor list is sometimes in numerical order, but that's accidental. If you want to sort it into order every time, be my guest. If you own Jim Donnelly's Tool Library, you can use this little routine to call ALLF and sort the output: %%HP:T(3); @ FSORT by Joe Horn; uses ALLF and Donnelly's Tool Library. \<< ALLF OBJ\-> SWAP OBJ\-> EVAL QSORT \->LIST SWAP \->TAG \>> INSTRUCTIONS: Same as ALLF, but output is always in order. ---------- Number theorists use lowercase-sigma-sub-zero(n) or d(n) to stand for the number of factors of n, and lowercase-sigma-sub-one(n) to stand for the sum of the factors of n. A quick and dirty program to generate these functions for any input is: %%HP:T(3); @ SIG01 by Joe Horn; uses ALLF. \<< ALLF LIST\-> DUP "\Gs0" \->TAG @ sig0 OVER 2 + ROLLD \->ARRY CNRM "\Gs1" \->TAG @ sig1 \>> INSTRUCTIONS: Enter x, press SIG01. See sig0(x) and sig1(x). Example: 28 SIG01 --> sig0: 6 (in level 2), sig1: 56 (in level 1). This means that 28 has 6 divisors, which add up to 56. Note: These programs, together with PHI posted previously, totally replace and extend the TOTIENT tables in the CRC Standard Mathematical Tables handbook. The ancient Greeks enjoyed playing with numbers, and invented the concepts of Perfect, Abundant, and Deficient Numbers. "Perfect numbers" are those for which sig1(x)=2x. "Deficient numbers" are those for which sig1(x)<2x. "Abundant numbers" are those for which sig1(x)>2x. A more recent concept is the "Wierd Number", defined as those abundant numbers which have the wierd property of being unable to produce 2x by the sum of any subset of x's divisors. A program to check the wierdness of a number is left as an exercise to the student... -Joseph K. Horn- -Peripheral Vision, Ltd.-