/usr/share/doc/apcalc-common/examples/powerterm is in apcalc-common 2.12.4.4-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #!/usr/bin/calc -q -s -f
/*
* powerterm - print the argument as a sum of powers of integers
*
* usage:
* powerterm [base_limit] value
*
* base_limit largest base we will consider (def: 10000)
* value value to convert into sums of powers of integers
*
* Copyright (C) 2001 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the powerterm of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @(#) $Revision: 30.1 $
* @(#) $Id: powerterm.calc,v 30.1 2007/03/16 11:12:11 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/cscript/RCS/powerterm.calc,v $
*
* Under source code control: 2001/04/24 23:49:11
* File existed as early as: 2001
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* parse args
*/
config("verbose_quit", 0),;
base_lim = 10000; /* default: highest base we will consider */
if (argv() < 2 || argv() > 3) {
fprintf(files(2), "usage: %s [base_limit] value\n", argv(0));
exit;
}
if (argv() == 3) {
x = eval(argv(2));
base_lim = eval(argv(1));
} else {
x = eval(argv(1));
}
if (! isint(x)) {
fprintf(files(2), "%s: value must be an integer\n");
exit;
}
if (! isint(base_lim)) {
fprintf(files(2), "%s: base limit must be an integer\n");
exit;
}
if (base_lim <= 1) {
fprintf(files(2), "%s: base limit is too small\n");
exit;
}
++base_lim;
/*
* setup loop variables
*/
term = 0; /* number of powerterm found */
/*
* log constants
*/
if (base_lim <= 2^20+1) { /* 2^20 requires ~96 Megs of memory */
mat lni[base_lim]; /* log of integers */
for (i=2; i < base_lim; ++i) {
lni[i] = ln(i);
}
have_lni = 1; /* have lni[x] array */
} else {
mat lni[1]; /* not used */
have_lni = 0; /* base_lim too large for array */
}
/*
* remove nestest powers
*/
while (abs(x) >= base_lim) {
/*
* look for the nearest power
*/
lnx = ln(abs(x)); /* log of the remaining co-factor */
closest = 0.5;
base = 1;
exponent = 0;
if (have_lni) {
/*
* use pre-calculated log array when looking for the nearest power
*/
for (i = 2; i < base_lim; ++i) {
/*
* determine exponent closeness to an integer
*/
ex = lnx / lni[i];
power = int(ex + 0.5);
diff = ex - power;
/*
* look for a closer power
*/
if (abs(diff) < closest) {
closest = abs(diff);
base = i;
exponent = power;
}
}
} else {
/*
* re-calculate logs when looking for the nearest power
*/
for (i = 2; i < base_lim; ++i) {
/*
* determine exponent closeness to an integer
*/
ex = lnx / ln(i);
power = int(ex + 0.5);
diff = ex - power;
/*
* look for a closer power
*/
if (abs(diff) < closest) {
closest = abs(diff);
base = i;
exponent = power;
}
}
}
/*
* output current term and then subtract it
*/
if (x != 0) {
if (x < 0) {
print "-",;
} else if (term > 0) {
print "+",;
}
if (exponent > 1) {
print base: "^": exponent,;
} else {
print base,;
}
}
/*
* subtract (or add) this near power
*/
if (x < 0) {
x = x + base^exponent;
} else {
x = x - base^exponent;
}
++term;
}
/*
* print the final term
*/
if (x < 0) {
print "-", -x;
} else if (x > 0) {
print "+", x;
} else {
print "";
}
exit;
|