Mercurial > hg > octave-lyh
annotate scripts/specfun/factor.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | c1fff751b5a8 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2009 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5827 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{p} =} factor (@var{q}) | |
21 ## @deftypefnx {Function File} {[@var{p}, @var{n}] =} factor (@var{q}) | |
22 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## Return prime factorization of @var{q}. That is @code{prod (@var{p}) |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## == @var{q}}. If @code{@var{q} == 1}, returns 1. |
5827 | 25 ## |
7001 | 26 ## With two output arguments, returns the unique primes @var{p} and |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## their multiplicities. That is @code{prod (@var{p} .^ @var{n}) == |
6248 | 28 ## @var{q}}. |
5827 | 29 ## |
30 ## @end deftypefn | |
31 | |
32 ## Author: Paul Kienzle | |
33 | |
34 ## 2002-01-28 Paul Kienzle | |
35 ## * remove recursion; only check existing primes for multiplicity > 1 | |
36 ## * return multiplicity as suggested by Dirk Laurie | |
37 ## * add error handling | |
38 | |
39 function [x, m] = factor (n) | |
40 | |
41 if (nargin < 1) | |
42 print_usage (); | |
43 endif | |
44 | |
45 if (! isscalar (n) || n != fix (n)) | |
46 error ("factor: n must be a scalar integer"); | |
47 endif | |
48 | |
8506 | 49 ## Special case of no primes less than sqrt(n). |
5827 | 50 if (n < 4) |
51 x = n; | |
52 m = 1; | |
53 return; | |
54 endif | |
55 | |
56 x = []; | |
57 ## There is at most one prime greater than sqrt(n), and if it exists, | |
58 ## it has multiplicity 1, so no need to consider any factors greater | |
59 ## than sqrt(n) directly. [If there were two factors p1, p2 > sqrt(n), | |
60 ## then n >= p1*p2 > sqrt(n)*sqrt(n) == n. Contradiction.] | |
61 p = primes (sqrt (n)); | |
62 while (n > 1) | |
8506 | 63 ## Find prime factors in remaining n. |
5827 | 64 q = n ./ p; |
65 p = p (q == fix (q)); | |
66 if (isempty (p)) | |
8506 | 67 ## Can't be reduced further, so n must itself be a prime. |
68 p = n; | |
5827 | 69 endif |
70 x = [x, p]; | |
8506 | 71 ## Reduce n. |
5827 | 72 n = n / prod (p); |
73 endwhile | |
74 x = sort (x); | |
75 | |
8506 | 76 ## Determine muliplicity. |
5827 | 77 if (nargout > 1) |
78 idx = find ([0, x] != [x, 0]); | |
79 x = x(idx(1:length(idx)-1)); | |
80 m = diff (idx); | |
81 endif | |
82 | |
83 endfunction | |
84 | |
85 ## test: | |
86 ## assert(factor(1),1); | |
87 ## for i=2:20 | |
88 ## p = factor(i); | |
89 ## assert(prod(p),i); | |
90 ## assert(all(isprime(p))); | |
91 ## [p,n] = factor(i); | |
92 ## assert(prod(p.^n),i); | |
93 ## assert(all([0,p]!=[p,0])); | |
94 ## end |