|
|
|
|
@ -3180,6 +3180,33 @@ namespace eval punk::lib {
|
|
|
|
|
#[para]Use expr's log10() function or tcl::mathfunc::log10 for base 10 |
|
|
|
|
expr {log($x)/log($b)} |
|
|
|
|
} |
|
|
|
|
namespace eval argdoc { |
|
|
|
|
variable PUNKARGS |
|
|
|
|
lappend PUNKARGS [list { |
|
|
|
|
@id -id ::punk::lib::factors |
|
|
|
|
@cmd -name punk::lib::factors\ |
|
|
|
|
-summary\ |
|
|
|
|
"Sorted factors of positive integer x"\ |
|
|
|
|
-help\ |
|
|
|
|
"Return a sorted list of the positive factors of x where x > 0 |
|
|
|
|
For x = 0 we return only 0 and 1 as technically any number divides zero and there are an infinite number of factors. |
|
|
|
|
(including zero itself in this context)* |
|
|
|
|
|
|
|
|
|
This is a simple brute-force implementation that iterates all numbers below the square root of x to check the factors |
|
|
|
|
Because the implementation is so simple - the performance is very reasonable for numbers below at least a few 10's of millions |
|
|
|
|
See tcllib math::numtheory::factors for a more complex implementation - which seems to be slower for 'small' numbers |
|
|
|
|
Comparisons were done with some numbers below 17 digits long |
|
|
|
|
For seriously big numbers - this simple algorithm would no doubt be outperformed by more complex algorithms. |
|
|
|
|
The numtheory library stores some data about primes etc with each call - so may become faster when being used on more numbers |
|
|
|
|
but has the disadvantage of being slower for 'small' numbers and using more memory. |
|
|
|
|
If the largest factor below x is needed - the greatestOddFactorBelow and GreatestFactorBelow functions are a faster way to get |
|
|
|
|
there than computing the whole list, even for small values of x |
|
|
|
|
* Taking x=0; Notion of x being divisible by integer y being: There exists an integer p such that x = py |
|
|
|
|
In other mathematical contexts zero may be considered not to divide anything." |
|
|
|
|
@values -min 1 -max 1 |
|
|
|
|
x -type integer |
|
|
|
|
}] |
|
|
|
|
} |
|
|
|
|
proc factors {x} { |
|
|
|
|
#*** !doctools |
|
|
|
|
#[call [fun factors] [arg x]] |
|
|
|
|
@ -3293,6 +3320,25 @@ namespace eval punk::lib {
|
|
|
|
|
} |
|
|
|
|
return $r |
|
|
|
|
} |
|
|
|
|
namespace eval argdoc { |
|
|
|
|
variable PUNKARGS |
|
|
|
|
lappend PUNKARGS [list { |
|
|
|
|
@id -id ::punk::lib::gcd |
|
|
|
|
@cmd -name punk::lib::gcd\ |
|
|
|
|
-summary\ |
|
|
|
|
"Gretest common divisor of m and n."\ |
|
|
|
|
-help\ |
|
|
|
|
"Return the greatest common divisor of m and n. |
|
|
|
|
Straight from Lars Hellström's math::numtheory library in Tcllib |
|
|
|
|
|
|
|
|
|
Graphical use: |
|
|
|
|
An a by b rectangle can be covered with square tiles of side-length c, |
|
|
|
|
only if c is a common divisor of a and b" |
|
|
|
|
@values -min 2 -max 2 |
|
|
|
|
m -type integer |
|
|
|
|
n -type integer |
|
|
|
|
}] |
|
|
|
|
} |
|
|
|
|
proc gcd {n m} { |
|
|
|
|
#*** !doctools |
|
|
|
|
#[call [fun gcd] [arg n] [arg m]] |
|
|
|
|
|