A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
15
Task/Multifactorial/0DESCRIPTION
Normal file
15
Task/Multifactorial/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
The factorial of a number, written as <math>n!</math> is defined as <math>n! = n(n-1)(n-2)...(2)(1)</math>
|
||||
|
||||
A generalization of this is the [http://mathworld.wolfram.com/Multifactorial.html multifactorials] where:
|
||||
: <math>n! = n(n-1)(n-2)...(2)(1)</math>
|
||||
: <math>n!! = n(n-2)(n-4)...</math>
|
||||
: <math>n!! ! = n(n-3)(n-6)...</math>
|
||||
: <math>n!! !! = n(n-4)(n-8)...</math>
|
||||
: <math>n!! !! ! = n(n-5)(n-10)...</math>
|
||||
: Where the products are for positive integers.
|
||||
|
||||
If we define the degree of the multifactorial as the difference in successive terms that are multiplied together for a multifactorial (The number of exclamation marks) then the task is to
|
||||
# Write a function that given n and the degree, calculates the multifactorial.
|
||||
# Use the function to generate and display here a table of the first 1..10 members of the first five degrees of multifactorial.
|
||||
|
||||
<small>'''Note:''' The [[wp:Factorial#Multifactorials|wikipedia entry on multifactorials]] gives a different formula. This task uses the [http://mathworld.wolfram.com/Multifactorial.html Wolfram mathworld definition].</small>
|
||||
19
Task/Multifactorial/Ada/multifactorial.ada
Normal file
19
Task/Multifactorial/Ada/multifactorial.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
procedure Mfact is
|
||||
|
||||
function MultiFact (num : Natural; deg : Positive) return Natural is
|
||||
Result, N : Integer := num;
|
||||
begin
|
||||
if N = 0 then return 1; end if;
|
||||
loop
|
||||
N := N - deg; exit when N <= 0; Result := Result * N;
|
||||
end loop; return Result;
|
||||
end MultiFact;
|
||||
|
||||
begin
|
||||
for deg in 1..5 loop
|
||||
Put("Degree"& Integer'Image(deg) &":");
|
||||
for num in 1..10 loop Put(Integer'Image(MultiFact(num,deg))); end loop;
|
||||
New_line;
|
||||
end loop;
|
||||
end Mfact;
|
||||
16
Task/Multifactorial/C++/multifactorial.cpp
Normal file
16
Task/Multifactorial/C++/multifactorial.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
/*Generate multifactorials to 9
|
||||
|
||||
Nigel_Galloway
|
||||
November 14th., 2012.
|
||||
*/
|
||||
int main(void) {
|
||||
for (int g = 1; g < 10; g++) {
|
||||
int v[11], n=0;
|
||||
generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;});
|
||||
std::cout << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
30
Task/Multifactorial/C/multifactorial.c
Normal file
30
Task/Multifactorial/C/multifactorial.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* Include statements and constant definitions */
|
||||
#include <stdio.h>
|
||||
#define HIGHEST_DEGREE 5
|
||||
#define LARGEST_NUMBER 10
|
||||
|
||||
/* Recursive implementation of multifactorial function */
|
||||
int multifact(int n, int deg){
|
||||
return n <= deg ? n : n * multifact(n - deg, deg);
|
||||
}
|
||||
|
||||
/* Iterative implementation of multifactorial function */
|
||||
int multifact_i(int n, int deg){
|
||||
int result = n;
|
||||
while (n >= deg + 1){
|
||||
result *= (n - deg);
|
||||
n -= deg;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Test function to print out multifactorials */
|
||||
int main(void){
|
||||
int i, j;
|
||||
for (i = 1; i <= HIGHEST_DEGREE; i++){
|
||||
printf("\nDegree %d: ", i);
|
||||
for (j = 1; j <= LARGEST_NUMBER; j++){
|
||||
printf("%d ", multifact(j, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Task/Multifactorial/Clojure/multifactorial.clj
Normal file
5
Task/Multifactorial/Clojure/multifactorial.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defn !! [m n]
|
||||
(apply * (take-while pos? (iterate #(- % m) n))))
|
||||
|
||||
(doseq [m (range 1 6)]
|
||||
(prn m (map #(!! m %) (range 1 11))))
|
||||
7
Task/Multifactorial/Common-Lisp/multifactorial.lisp
Normal file
7
Task/Multifactorial/Common-Lisp/multifactorial.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defun mfac (n m)
|
||||
(reduce #'* (loop for i from n downto 1 by m collect i)))
|
||||
|
||||
(loop for i from 1 to 10
|
||||
do (format t "~2@a: ~{~a~^ ~}~%"
|
||||
i (loop for j from 1 to 10
|
||||
collect (mfac j i))))
|
||||
12
Task/Multifactorial/D/multifactorial.d
Normal file
12
Task/Multifactorial/D/multifactorial.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
T multifactorial(T=long)(in int n, in int m) /*pure*/ {
|
||||
T one = 1;
|
||||
return reduce!q{a * b}(one, iota(n, 0, -m));
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (m; 1 .. 11)
|
||||
writefln("%2d: %s", m, iota(1, 11)
|
||||
.map!(n => multifactorial(n, m))());
|
||||
}
|
||||
12
Task/Multifactorial/Erlang/multifactorial-1.erl
Normal file
12
Task/Multifactorial/Erlang/multifactorial-1.erl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-module(multifac).
|
||||
-compile(export_all).
|
||||
|
||||
multifac(N,D) ->
|
||||
lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)).
|
||||
|
||||
main() ->
|
||||
Ds = lists:seq(1,5),
|
||||
Ns = lists:seq(1,10),
|
||||
lists:foreach(fun (D) ->
|
||||
io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]])
|
||||
end, Ds).
|
||||
7
Task/Multifactorial/Erlang/multifactorial-2.erl
Normal file
7
Task/Multifactorial/Erlang/multifactorial-2.erl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
5> multifac:main().
|
||||
Degree 1: [1,2,6,24,120,720,5040,40320,362880,3628800]
|
||||
Degree 2: [1,2,3,8,15,48,105,384,945,3840]
|
||||
Degree 3: [1,2,3,4,10,18,28,80,162,280]
|
||||
Degree 4: [1,2,3,4,5,12,21,32,45,120]
|
||||
Degree 5: [1,2,3,4,5,6,14,24,36,50]
|
||||
ok
|
||||
21
Task/Multifactorial/Go/multifactorial.go
Normal file
21
Task/Multifactorial/Go/multifactorial.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func multiFactorial(n, k int) int {
|
||||
r := 1
|
||||
for ; n > 1; n -= k {
|
||||
r *= n
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
for k := 1; k <= 5; k++ {
|
||||
fmt.Print("degree ", k, ":")
|
||||
for n := 1; n <= 10; n++ {
|
||||
fmt.Print(" ", multiFactorial(n, k))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
6
Task/Multifactorial/Haskell/multifactorial.hs
Normal file
6
Task/Multifactorial/Haskell/multifactorial.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
mulfac k = 1:s where s = [1 .. k] ++ zipWith (*) s [k+1..]
|
||||
|
||||
-- for single n
|
||||
mulfac1 k n = product [n, n-k .. 1]
|
||||
|
||||
main = mapM_ (print . take 10 . tail . mulfac) [1..5]
|
||||
21
Task/Multifactorial/J/multifactorial.j
Normal file
21
Task/Multifactorial/J/multifactorial.j
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
NB. tacit implementation of the recursive c function
|
||||
NB. int multifact(int n,int deg){return n<=deg?n:n*multifact(n-deg,deg);}
|
||||
|
||||
multifact=: [`([ * - $: ])@.(<~)
|
||||
(a:,<' degree'),multifact table >:i.10
|
||||
┌─────────┬──────────────────────────────────────┐
|
||||
│ │ degree │
|
||||
├─────────┼──────────────────────────────────────┤
|
||||
│multifact│ 1 2 3 4 5 6 7 8 9 10│
|
||||
├─────────┼──────────────────────────────────────┤
|
||||
│ 1 │ 1 1 1 1 1 1 1 1 1 1│
|
||||
│ 2 │ 2 2 2 2 2 2 2 2 2 2│
|
||||
│ 3 │ 6 3 3 3 3 3 3 3 3 3│
|
||||
│ 4 │ 24 8 4 4 4 4 4 4 4 4│
|
||||
│ 5 │ 120 15 10 5 5 5 5 5 5 5│
|
||||
│ 6 │ 720 48 18 12 6 6 6 6 6 6│
|
||||
│ 7 │ 5040 105 28 21 14 7 7 7 7 7│
|
||||
│ 8 │ 40320 384 80 32 24 16 8 8 8 8│
|
||||
│ 9 │ 362880 945 162 45 36 27 18 9 9 9│
|
||||
│10 │3628800 3840 280 120 50 40 30 20 10 10│
|
||||
└─────────┴──────────────────────────────────────┘
|
||||
8
Task/Multifactorial/JavaScript/multifactorial-1.js
Normal file
8
Task/Multifactorial/JavaScript/multifactorial-1.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function multifact(n, deg){
|
||||
var result = n;
|
||||
while (n >= deg + 1){
|
||||
result *= (n - deg);
|
||||
n -= deg;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
9
Task/Multifactorial/JavaScript/multifactorial-2.js
Normal file
9
Task/Multifactorial/JavaScript/multifactorial-2.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function test (n, deg) {
|
||||
for (var i = 1; i <= deg; i ++) {
|
||||
var results = '';
|
||||
for (var j = 1; j <= n; j ++) {
|
||||
results += multifact(j, i) + ' ';
|
||||
}
|
||||
console.log('Degree ' + i + ': ' + results);
|
||||
}
|
||||
}
|
||||
6
Task/Multifactorial/JavaScript/multifactorial-3.js
Normal file
6
Task/Multifactorial/JavaScript/multifactorial-3.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
test(10, 5)
|
||||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800
|
||||
Degree 2: 1 2 3 8 15 48 105 384 945 3840
|
||||
Degree 3: 1 2 3 4 10 18 28 80 162 280
|
||||
Degree 4: 1 2 3 4 5 12 21 32 45 120
|
||||
Degree 5: 1 2 3 4 5 6 14 24 36 50
|
||||
3
Task/Multifactorial/JavaScript/multifactorial-4.js
Normal file
3
Task/Multifactorial/JavaScript/multifactorial-4.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
function multifact(n, deg){
|
||||
return n <= deg ? n : n * multifact(n - deg, deg);
|
||||
}
|
||||
9
Task/Multifactorial/JavaScript/multifactorial-5.js
Normal file
9
Task/Multifactorial/JavaScript/multifactorial-5.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function test (n, deg) {
|
||||
for (var i = 1; i <= deg; i ++) {
|
||||
var results = '';
|
||||
for (var j = 1; j <= n; j ++) {
|
||||
results += multifact(j, i) + ' ';
|
||||
}
|
||||
console.log('Degree ' + i + ': ' + results);
|
||||
}
|
||||
}
|
||||
6
Task/Multifactorial/JavaScript/multifactorial-6.js
Normal file
6
Task/Multifactorial/JavaScript/multifactorial-6.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
test(10, 5)
|
||||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800
|
||||
Degree 2: 1 2 3 8 15 48 105 384 945 3840
|
||||
Degree 3: 1 2 3 4 10 18 28 80 162 280
|
||||
Degree 4: 1 2 3 4 5 12 21 32 45 120
|
||||
Degree 5: 1 2 3 4 5 6 14 24 36 50
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Multifactorial[n_, m_] := Abs[ Apply[ Times, Range[-n, -1, m]]]
|
||||
Table[ Multifactorial[j, i], {i, 5}, {j, 10}] // TableForm
|
||||
8
Task/Multifactorial/Perl/multifactorial-1.pl
Normal file
8
Task/Multifactorial/Perl/multifactorial-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use 5.10.0;
|
||||
|
||||
sub ng {
|
||||
state %g;
|
||||
my ($n, $d, $key) = ( @_[0], @_[1], $n.'ng'.$d);
|
||||
if (!$g{$key}) {$g[$key] = ($n <= $d+1)? $n : ng($n-$d,$d)*$n}
|
||||
return $g[$key];
|
||||
}
|
||||
9
Task/Multifactorial/Perl/multifactorial-2.pl
Normal file
9
Task/Multifactorial/Perl/multifactorial-2.pl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,1), ng(2,1), ng(3,1), ng(4,1), ng(5,1), ng(6,1), ng(7,1), ng(8,1), ng(9,1), ng(10,1) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,2), ng(2,2), ng(3,2), ng(4,2), ng(5,2), ng(6,2), ng(7,2), ng(8,2), ng(9,2), ng(10,2) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,3), ng(2,3), ng(3,3), ng(4,3), ng(5,3), ng(6,3), ng(7,3), ng(8,3), ng(9,3), ng(10,3) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,4), ng(2,4), ng(3,4), ng(4,4), ng(5,4), ng(6,4), ng(7,4), ng(8,4), ng(9,4), ng(10,4) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,5), ng(2,5), ng(3,5), ng(4,5), ng(5,5), ng(6,5), ng(7,5), ng(8,5), ng(9,5), ng(10,5) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,6), ng(2,6), ng(3,6), ng(4,6), ng(5,6), ng(6,6), ng(7,6), ng(8,6), ng(9,6), ng(10,6) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,7), ng(2,7), ng(3,7), ng(4,7), ng(5,7), ng(6,7), ng(7,7), ng(8,7), ng(9,7), ng(10,7) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,8), ng(2,8), ng(3,8), ng(4,8), ng(5,8), ng(6,8), ng(7,8), ng(8,8), ng(9,8), ng(10,8) ;
|
||||
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,9), ng(2,9), ng(3,9), ng(4,9), ng(5,9), ng(6,9), ng(7,9), ng(8,9), ng(9,9), ng(10,9) ;
|
||||
17
Task/Multifactorial/Python/multifactorial-1.py
Normal file
17
Task/Multifactorial/Python/multifactorial-1.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
>>> from functools import reduce
|
||||
>>> from operator import mul
|
||||
>>> def mfac(n, m): return reduce(mul, range(n, 0, -m))
|
||||
|
||||
>>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)]))
|
||||
|
||||
1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
|
||||
2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840]
|
||||
3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280]
|
||||
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120]
|
||||
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50]
|
||||
6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40]
|
||||
7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30]
|
||||
8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
|
||||
9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
>>>
|
||||
10
Task/Multifactorial/Python/multifactorial-2.py
Normal file
10
Task/Multifactorial/Python/multifactorial-2.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
>>> def mfac2(n, m): return n if n <= (m + 1) else n * mfac2(n - m, m)
|
||||
|
||||
>>> for m in range(1, 6): print("%2i: %r" % (m, [mfac2(n, m) for n in range(1, 11)]))
|
||||
|
||||
1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
|
||||
2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840]
|
||||
3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280]
|
||||
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120]
|
||||
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50]
|
||||
>>>
|
||||
20
Task/Multifactorial/REXX/multifactorial.rexx
Normal file
20
Task/Multifactorial/REXX/multifactorial.rexx
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX pgm calculates K-fact (multifactorial) of non-negative integers.*/
|
||||
numeric digits 1000 /*lets get ka-razy with precision*/
|
||||
parse arg num deg . /*allow user to specify num & deg*/
|
||||
if num=='' | num==',' then num=12 /*Not specified? Then use default*/
|
||||
if deg=='' | deg==',' then deg=10 /* " " " " " */
|
||||
|
||||
do d=1 to deg /*the degree of factorialization.*/
|
||||
_= /*the list of factorials so far. */
|
||||
do f=1 to num
|
||||
_=_ Kfact(f,d) /*construct a list of factorials.*/
|
||||
end /*f*/ /*(above) D can be omitted. */
|
||||
|
||||
say 'degree' right(d,length(num))':' _ /*show factorials.*/
|
||||
end /*d*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────KFACT subroutine────────────────────*/
|
||||
Kfact: procedure; !=1; do j=arg(1) to 2 by -word(arg(2) 1, 1)
|
||||
!=!*j
|
||||
end /*j*/
|
||||
return !
|
||||
18
Task/Multifactorial/Racket/multifactorial.rkt
Normal file
18
Task/Multifactorial/Racket/multifactorial.rkt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#lang racket
|
||||
|
||||
(define (multi-factorial-fn m)
|
||||
(lambda (n)
|
||||
(let inner ((acc 1) (n n))
|
||||
(if (<= n m) (* acc n)
|
||||
(inner (* acc n) (- n m))))))
|
||||
|
||||
;; using (multi-factorial-fn m) as a first-class function
|
||||
(for*/list ([m (in-range 1 (add1 5))] [mf-m (in-value (multi-factorial-fn m))])
|
||||
(for/list ([n (in-range 1 (add1 10))])
|
||||
(mf-m n)))
|
||||
|
||||
(define (multi-factorial m n) ((multi-factorial-fn m) n))
|
||||
|
||||
(for/list ([m (in-range 1 (add1 5))])
|
||||
(for/list ([n (in-range 1 (add1 10))])
|
||||
(multi-factorial m n)))
|
||||
5
Task/Multifactorial/Ruby/multifactorial.rb
Normal file
5
Task/Multifactorial/Ruby/multifactorial.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def multifact(n, d)
|
||||
n.step(1, -d).inject( :* )
|
||||
end
|
||||
|
||||
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}
|
||||
11
Task/Multifactorial/Tcl/multifactorial.tcl
Normal file
11
Task/Multifactorial/Tcl/multifactorial.tcl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package require Tcl 8.6
|
||||
|
||||
proc mfact {n m} {
|
||||
set mm [expr {-$m}]
|
||||
for {set r $n} {[incr n $mm] > 1} {set r [expr {$r * $n}]} {}
|
||||
return $r
|
||||
}
|
||||
|
||||
foreach n {1 2 3 4 5 6 7 8 9 10} {
|
||||
puts $n:[join [lmap m {1 2 3 4 5 6 7 8 9 10} {mfact $m $n}] ,]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue