Sunday, October 10, 2021

n = a^m + b

 n = a^m + b



Calculators:  Swiss Micros DM41X, HP-32S/32SII, Casio fx-9750 GIII


n = a^2 + b


The following program breaks down a positive integer n into n = a^2 + b, where a is √n rounded to the nearest integer in attempt to minimize abs(b).    


Examples:

  

800 = 28^2 + 16

662 = 26^2 - 14

525 = 23^2 - 4


This technique is useful, especially with the HP 32S and HP 32SII.  Normally, real numbers take up 9.5 bytes of memory on the HP 32S series.  Integers 0 - 100 only consume 1.5 bytes of memory for the original HP 32S and the range is extended to 0 - 254 for the HP 32SII.  


When the program terminates, b is on the X stack while a is on the Y stack.  


HP 41C/Swiss Micros DM41X Program:  SQPLUS


01 LBL^T SQPLUS

02 FIX 0

03 ENTER 

04 SQRT

05 RND

06 X^2

07 -

08 LASTX

09 SQRT

10 X<>Y

11 FIX 4

12 RTN


HP 32S and HP 32SII Program: SQPLUS


S01 LBL S

S02 FIX 0

S03 ENTER

S04 SQRT

S05 RND

S06 x^2

S07 -

S08 LASTx

S09 SQRT

S10 x<>y

S11 ALL

S12 RTN


Example:  684


Result:

Y:  26

X:  8 

(26^2 + 8 = 684)


Additional Powers


Now let's extend the problem to include higher powers.  


n = a^m + b


We can estimate a by:


n ≈ a^m

ln n ≈ ln(a^m)

ln n ≈ m * ln a

ln n / m ≈ ln a


a ≈ e^(ln n / m)


Round a to the nearest integer.  Use this a to find b.


n = a^m + b


b = n - a^m


The script brkpower.py (225 bytes) calculates a and b for powers m = 2 to 6.  This script was created on the Casio fx-9750GIII.


Casio fx-9750GIII Micro python Script:  brkpower.py


# 2021-07-17 ews

from math import *

print("n = a**m + b")

n=float(input("n? "))

m=2.0

while m<7:

  a=int(exp(log(n)/m)+.5)

  b=n-a**m

  s="{:.0f} = {:.0f}**{:.0f} + {:.of}".format(n,a,m,b)

  print(s)

  m+=1


Example:  n = 860


Results:

860 = 29**2 + 19         (860 = 29^2 + 19) 

860 = 10**3 + -140     (860 = 10^3 - 140, and so on)

860 = 5**4 + 235

860 = 4**5 + -164

860 = 3**6 + 131


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.