1 #!/usr/local/src/cmd/calc/calc -q -s -f
3 * powerterm - print the argument as a sum of powers of integers
6 * powerterm [base_limit] value
8 * base_limit largest base we will consider (def: 10000)
9 * value value to convert into sums of powers of integers
11 * Copyright (C) 2001 Landon Curt Noll
13 * Calc is open software; you can redistribute it and/or modify it under
14 * the powerterm of the version 2.1 of the GNU Lesser General Public License
15 * as published by the Free Software Foundation.
17 * Calc is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
20 * Public License for more details.
22 * A copy of version 2.1 of the GNU Lesser General Public License is
23 * distributed with calc under the filename COPYING-LGPL. You should have
24 * received a copy with calc; if not, write to Free Software Foundation, Inc.
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * @(#) $Revision: 30.1 $
28 * @(#) $Id: powerterm.calc,v 30.1 2007/03/16 11:12:11 chongo Exp $
29 * @(#) $Source: /usr/local/src/bin/calc/cscript/RCS/powerterm.calc,v $
31 * Under source code control: 2001/04/24 23:49:11
32 * File existed as early as: 2001
34 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
35 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
42 config("verbose_quit", 0),;
43 base_lim = 10000; /* default: highest base we will consider */
44 if (argv() < 2 || argv() > 3) {
45 fprintf(files(2), "usage: %s [base_limit] value\n", argv(0));
50 base_lim = eval(argv(1));
55 fprintf(files(2), "%s: value must be an integer\n");
58 if (! isint(base_lim)) {
59 fprintf(files(2), "%s: base limit must be an integer\n");
63 fprintf(files(2), "%s: base limit is too small\n");
69 * setup loop variables
71 term = 0; /* number of powerterm found */
76 if (base_lim <= 2^20+1) { /* 2^20 requires ~96 Megs of memory */
77 mat lni[base_lim]; /* log of integers */
78 for (i=2; i < base_lim; ++i) {
81 have_lni = 1; /* have lni[x] array */
83 mat lni[1]; /* not used */
84 have_lni = 0; /* base_lim too large for array */
88 * remove nestest powers
90 while (abs(x) >= base_lim) {
93 * look for the nearest power
95 lnx = ln(abs(x)); /* log of the remaining co-factor */
102 * use pre-calculated log array when looking for the nearest power
104 for (i = 2; i < base_lim; ++i) {
107 * determine exponent closeness to an integer
110 power = int(ex + 0.5);
114 * look for a closer power
116 if (abs(diff) < closest) {
126 * re-calculate logs when looking for the nearest power
128 for (i = 2; i < base_lim; ++i) {
131 * determine exponent closeness to an integer
134 power = int(ex + 0.5);
138 * look for a closer power
140 if (abs(diff) < closest) {
149 * output current term and then subtract it
154 } else if (term > 0) {
158 print base: "^": exponent,;
165 * subtract (or add) this near power
168 x = x + base^exponent;
170 x = x - base^exponent;
176 * print the final term