1/* 2 * unixfrac - represent a fraction as a sum of distance unit fractions 3 * 4 * Copyright (C) 1999,2021 David I. Bell 5 * 6 * Calc is open software; you can redistribute it and/or modify it under 7 * the terms of the version 2.1 of the GNU Lesser General Public License 8 * as published by the Free Software Foundation. 9 * 10 * Calc is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 13 * Public License for more details. 14 * 15 * A copy of version 2.1 of the GNU Lesser General Public License is 16 * distributed with calc under the filename COPYING-LGPL. You should have 17 * received a copy with calc; if not, write to Free Software Foundation, Inc. 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * 20 * Under source code control: 1990/02/15 01:50:38 21 * File existed as early as: before 1990 22 * 23 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ 24 */ 25 26/* 27 * Represent a fraction as sum of distinct unit fractions. 28 * The output is the unit fractions themselves, and in square brackets, 29 * the number of digits in the numerator and denominator of the value left 30 * to be found. Numbers larger than 3.5 become very difficult to calculate. 31 */ 32 33 34define unitfrac(x) 35{ 36 local d, di, n; 37 38 if (x <= 0) 39 quit "Non-positive argument"; 40 d = 2; 41 do { 42 n = int(1 / x) + 1; 43 if (n > d) 44 d = n; 45 di = 1/d; 46 print ' [': digits(num(x)): '/': digits(den(x)): ']',, di; 47 x -= di; 48 d++; 49 } while ((num(x) > 1) || (x == di) || (x == 1)); 50 print ' [1/1]',, x; 51} 52