September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
176
Task/Factorial/ARM-Assembly/factorial.arm
Normal file
176
Task/Factorial/ARM-Assembly/factorial.arm
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program factorial.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessLargeNumber: .asciz "Number N to large. \n"
|
||||
szMessNegNumber: .asciz "Number N is negative. \n"
|
||||
|
||||
szMessResult: .ascii "Resultat = " @ message result
|
||||
sMessValeur: .fill 12, 1, ' '
|
||||
.asciz "\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
push {fp,lr} @ saves 2 registers
|
||||
|
||||
mov r0,#-5
|
||||
bl factorial
|
||||
mov r0,#10
|
||||
bl factorial
|
||||
mov r0,#20
|
||||
bl factorial
|
||||
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur 2 registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
|
||||
/********************************************/
|
||||
/* calculation */
|
||||
/********************************************/
|
||||
/* r0 contains number N */
|
||||
factorial:
|
||||
push {r1,r2,lr} @ save registres
|
||||
cmp r0,#0
|
||||
blt 99f
|
||||
beq 100f
|
||||
cmp r0,#1
|
||||
beq 100f
|
||||
bl calFactorial
|
||||
cmp r0,#-1 @ overflow ?
|
||||
beq 98f
|
||||
ldr r1,iAdrsMessValeur
|
||||
bl conversion10 @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
b 100f
|
||||
|
||||
98: @ display error message
|
||||
ldr r0,iAdrszMessLargeNumber
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99: @ display error message
|
||||
ldr r0,iAdrszMessNegNumber
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
pop {r1,r2,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
iAdrszMessNegNumber: .int szMessNegNumber
|
||||
iAdrszMessLargeNumber: .int szMessLargeNumber
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
iAdrszMessResult: .int szMessResult
|
||||
/******************************************************************/
|
||||
/* calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the number N */
|
||||
calFactorial:
|
||||
cmp r0,#1 @ N = 1 ?
|
||||
bxeq lr @ yes -> return
|
||||
push {fp,lr} @ save registers
|
||||
sub sp,#4 @ 4 byte on the stack
|
||||
mov fp,sp @ fp <- start address stack
|
||||
str r0,[fp] @ fp contains N
|
||||
sub r0,#1 @ call function with N - 1
|
||||
bl calFactorial
|
||||
cmp r0,#-1 @ error overflow ?
|
||||
beq 100f @ yes -> return
|
||||
ldr r1,[fp] @ load N
|
||||
umull r0,r2,r1,r0 @ multiply result by N
|
||||
cmp r2,#0 @ r2 is the hi rd if <> 0 overflow
|
||||
movne r0,#-1 @ if overflow -1 -> r0
|
||||
|
||||
100:
|
||||
add sp,#4 @ free 4 bytes on stack
|
||||
pop {fp,lr} @ restau2 registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save registres */
|
||||
push {r0,r1,r2,r7} /* save others registers */
|
||||
mov r2,#0 /* counter length */
|
||||
1: /* loop length calculation */
|
||||
ldrb r1,[r0,r2] /* read octet start position + index */
|
||||
cmp r1,#0 /* if 0 its over */
|
||||
addne r2,r2,#1 /* else add 1 in the length */
|
||||
bne 1b /* and loop */
|
||||
/* so here r2 contains the length of the message */
|
||||
mov r1,r0 /* address message in r1 */
|
||||
mov r0,#STDOUT /* code to write to the standard output Linux */
|
||||
mov r7, #WRITE /* code call system "write" */
|
||||
swi #0 /* call systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur others registers */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* return */
|
||||
/******************************************************************/
|
||||
/* Converting a register to a decimal */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
conversion10:
|
||||
push {r1-r4,lr} /* save registers */
|
||||
mov r3,r1
|
||||
mov r2,#10
|
||||
|
||||
1: @ start loop
|
||||
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
sub r2,#1 @ previous position
|
||||
cmp r0,#0 @ stop if quotient = 0 */
|
||||
bne 1b @ else loop
|
||||
@ and move spaves in first on area
|
||||
mov r1,#' ' @ space
|
||||
2:
|
||||
strb r1,[r3,r2] @ store space in area
|
||||
subs r2,#1 @ @ previous position
|
||||
bge 2b @ loop if r2 >= zéro
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} /* save registers */
|
||||
mov r4,r0
|
||||
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
|
||||
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
|
||||
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
|
||||
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
|
||||
add r0, r2, r1 /* r0 <- r2 + r1 */
|
||||
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
|
||||
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
|
||||
pop {r2-r4}
|
||||
bx lr /* leave function */
|
||||
.align 4
|
||||
.Ls_magic_number_10: .word 0x66666667
|
||||
|
||||
3
Task/Factorial/Agda/factorial.agda
Normal file
3
Task/Factorial/Agda/factorial.agda
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
factorial : ℕ → ℕ
|
||||
factorial zero = 1
|
||||
factorial (suc n) = suc n * factorial n
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
10 INPUT N
|
||||
20 LET FACT=1
|
||||
30 FOR I=2 TO N
|
||||
40 LET FACT=FACT*I
|
||||
50 NEXT I
|
||||
60 PRINT FACT
|
||||
100 DEF FACT(N)
|
||||
110 LET F=1
|
||||
120 FOR I=2 TO N
|
||||
130 LET F=F*I
|
||||
140 NEXT
|
||||
150 LET FACT=F
|
||||
160 END DEF
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
10 INPUT N
|
||||
20 LET FACT=1
|
||||
30 GOSUB 60
|
||||
40 PRINT FACT
|
||||
50 STOP
|
||||
60 IF N=0 THEN RETURN
|
||||
70 LET FACT=FACT*N
|
||||
80 LET N=N-1
|
||||
90 GOSUB 60
|
||||
100 RETURN
|
||||
30 FOR I=2 TO N
|
||||
40 LET FACT=FACT*I
|
||||
50 NEXT I
|
||||
60 PRINT FACT
|
||||
|
|
|
|||
10
Task/Factorial/BASIC/factorial-6.basic
Normal file
10
Task/Factorial/BASIC/factorial-6.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
10 INPUT N
|
||||
20 LET FACT=1
|
||||
30 GOSUB 60
|
||||
40 PRINT FACT
|
||||
50 STOP
|
||||
60 IF N=0 THEN RETURN
|
||||
70 LET FACT=FACT*N
|
||||
80 LET N=N-1
|
||||
90 GOSUB 60
|
||||
100 RETURN
|
||||
|
|
@ -1,14 +1,8 @@
|
|||
long long int Factorial(long long int m_nValue)
|
||||
{
|
||||
long long int result=m_nValue;
|
||||
long long int result_next;
|
||||
long long int pc = m_nValue;
|
||||
do
|
||||
{
|
||||
result_next = result*(pc-1);
|
||||
result = result_next;
|
||||
pc--;
|
||||
}while(pc>2);
|
||||
m_nValue = result;
|
||||
return m_nValue;
|
||||
}
|
||||
//iteration with while
|
||||
long long int factorial(long long int n)
|
||||
{
|
||||
long long int r = 1;
|
||||
while(1<n)
|
||||
r *= n--;
|
||||
return r;
|
||||
}
|
||||
|
|
|
|||
122
Task/Factorial/C++/factorial-4.cpp
Normal file
122
Task/Factorial/C++/factorial-4.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
|
||||
//bad style do-while and wrong for Factorial1(0LL) -> 0 !!!
|
||||
long long int Factorial1(long long int m_nValue)
|
||||
{
|
||||
long long int result=m_nValue;
|
||||
long long int result_next;
|
||||
long long int pc = m_nValue;
|
||||
do
|
||||
{
|
||||
result_next = result*(pc-1);
|
||||
result = result_next;
|
||||
pc--;
|
||||
}while(pc>2);
|
||||
m_nValue = result;
|
||||
return m_nValue;
|
||||
}
|
||||
|
||||
//iteration with while
|
||||
long long int Factorial2(long long int n)
|
||||
{
|
||||
long long int r = 1;
|
||||
while(1<n)
|
||||
r *= n--;
|
||||
return r;
|
||||
}
|
||||
|
||||
//recrusive
|
||||
long long int Factorial3(long long int n)
|
||||
{
|
||||
return n<2 ? 1 : n*Factorial3(n-1);
|
||||
}
|
||||
|
||||
//tail recursive
|
||||
inline long long int _fac_aux(long long int n, long long int acc) {
|
||||
return n < 1 ? acc : _fac_aux(n - 1, acc * n);
|
||||
}
|
||||
long long int Factorial4(long long int n)
|
||||
{
|
||||
return _fac_aux(n,1);
|
||||
}
|
||||
|
||||
//accumulate with functor
|
||||
long long int Factorial5(long long int n)
|
||||
{
|
||||
// last is one-past-end
|
||||
return std::accumulate(boost::counting_iterator<long long int>(1LL),
|
||||
boost::counting_iterator<long long int>(n+1LL), 1LL,
|
||||
std::multiplies<long long int>() );
|
||||
}
|
||||
|
||||
//accumulate with lamda
|
||||
long long int Factorial6(long long int n)
|
||||
{
|
||||
// last is one-past-end
|
||||
return std::accumulate(boost::counting_iterator<long long int>(1LL),
|
||||
boost::counting_iterator<long long int>(n+1LL), 1LL,
|
||||
[](long long int a, long long int b) { return a*b; } );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int v = 55;
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial1(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "do-while(1) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial2(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "while(2) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial3(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "recusive(3) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial3(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "tail recusive(4) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial5(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "std::accumulate(5) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
auto result = Factorial6(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> ms = t2 - t1;
|
||||
std::cout << std::fixed << "std::accumulate lamda(6) result " << result
|
||||
<< " took " << ms.count() << " ms\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,2 @@
|
|||
(defun fact (n)
|
||||
(if (< n 2)
|
||||
1
|
||||
(* n (fact(- n 1)))))
|
||||
(defun factorial (n)
|
||||
(if (zerop n) 1 (* n (factorial (1- n)))))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,2 @@
|
|||
(defun factorial (n)
|
||||
"Calculates N!"
|
||||
(loop for result = 1 then (* result i)
|
||||
for i from 2 to n
|
||||
finally (return result)))
|
||||
(defun factorial (n &optional (m 1))
|
||||
(if (zerop n) m (factorial (1- n) (* m n))))
|
||||
|
|
|
|||
|
|
@ -1,2 +1,5 @@
|
|||
(defun factorial (n)
|
||||
(reduce #'* (loop for i from 1 to n collect i)))
|
||||
"Calculates N!"
|
||||
(loop for result = 1 then (* result i)
|
||||
for i from 2 to n
|
||||
finally (return result)))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,2 @@
|
|||
;; Project : Factorial
|
||||
|
||||
(defun factorial (n)
|
||||
(cond ((= n 1) 1)
|
||||
(t (* n (factorial (- n 1))))))
|
||||
(format t "~a" "factorial of 8: ")
|
||||
(factorial 8)
|
||||
(reduce #'* (loop for i from 1 to n collect i)))
|
||||
|
|
|
|||
7
Task/Factorial/Common-Lisp/factorial-5.lisp
Normal file
7
Task/Factorial/Common-Lisp/factorial-5.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
;; Project : Factorial
|
||||
|
||||
(defun factorial (n)
|
||||
(cond ((= n 1) 1)
|
||||
(t (* n (factorial (- n 1))))))
|
||||
(format t "~a" "factorial of 8: ")
|
||||
(factorial 8)
|
||||
7
Task/Factorial/Dylan/factorial-1.dylan
Normal file
7
Task/Factorial/Dylan/factorial-1.dylan
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
else
|
||||
reduce1(\*, range(from: 1, to: n))
|
||||
end
|
||||
end method;
|
||||
11
Task/Factorial/Dylan/factorial-2.dylan
Normal file
11
Task/Factorial/Dylan/factorial-2.dylan
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
else
|
||||
let total = 1;
|
||||
for (i from n to 2 by -1)
|
||||
total := total * i;
|
||||
end;
|
||||
total
|
||||
end
|
||||
end method;
|
||||
13
Task/Factorial/Dylan/factorial-3.dylan
Normal file
13
Task/Factorial/Dylan/factorial-3.dylan
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
end;
|
||||
local method loop (n)
|
||||
if (n <= 2)
|
||||
n
|
||||
else
|
||||
n * loop(n - 1)
|
||||
end
|
||||
end;
|
||||
loop(n)
|
||||
end method;
|
||||
16
Task/Factorial/Dylan/factorial-4.dylan
Normal file
16
Task/Factorial/Dylan/factorial-4.dylan
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
end;
|
||||
// Dylan implementations are required to perform tail call optimization so
|
||||
// this is equivalent to iteration.
|
||||
local method loop (n, total)
|
||||
if (n <= 2)
|
||||
total
|
||||
else
|
||||
let next = n - 1;
|
||||
loop(next, total * next)
|
||||
end
|
||||
end;
|
||||
loop(n, n)
|
||||
end method;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
define method factorial(n)
|
||||
reduce1(\*, range(from: 1, to: n));
|
||||
end
|
||||
1
Task/Factorial/Excel/factorial.excel
Normal file
1
Task/Factorial/Excel/factorial.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=fact(5)
|
||||
40
Task/Factorial/Gambas/factorial.gambas
Normal file
40
Task/Factorial/Gambas/factorial.gambas
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
' Task: Factorial
|
||||
' Language: Gambas
|
||||
' Author: Sinuhe Masan (2019)
|
||||
' Function factorial iterative
|
||||
Function factorial_iter(num As Integer) As Long
|
||||
Dim fact As Long
|
||||
Dim i As Integer
|
||||
|
||||
fact = 1
|
||||
If num > 1 Then
|
||||
For i = 2 To num
|
||||
fact = fact * i
|
||||
|
||||
Next
|
||||
|
||||
Endif
|
||||
|
||||
Return fact
|
||||
|
||||
End
|
||||
|
||||
' Function factorial recursive
|
||||
Function factorial_rec(num As Integer) As Long
|
||||
|
||||
If num <= 1 Then
|
||||
Return 1
|
||||
|
||||
Else
|
||||
Return num * factorial_rec(num - 1)
|
||||
|
||||
Endif
|
||||
|
||||
End
|
||||
|
||||
|
||||
Public Sub Main()
|
||||
Print factorial_iter(6)
|
||||
Print factorial_rec(7)
|
||||
|
||||
End
|
||||
|
|
@ -1,11 +1,53 @@
|
|||
public static long fact(final int n) {
|
||||
if (n < 0) {
|
||||
System.err.println("No negative numbers");
|
||||
return 0;
|
||||
package programas;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class IterativeFactorial {
|
||||
|
||||
public BigInteger factorial(BigInteger n) {
|
||||
if ( n == null ) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
long ans = 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
ans *= i;
|
||||
else if ( n.signum() == - 1 ) {
|
||||
// negative
|
||||
throw new IllegalArgumentException("Argument must be a non-negative integer");
|
||||
}
|
||||
return ans;
|
||||
else {
|
||||
BigInteger factorial = BigInteger.ONE;
|
||||
for ( BigInteger i = BigInteger.ONE; i.compareTo(n) < 1; i = i.add(BigInteger.ONE) ) {
|
||||
factorial = factorial.multiply(i);
|
||||
}
|
||||
return factorial;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
BigInteger number, result;
|
||||
boolean error = false;
|
||||
System.out.println("FACTORIAL OF A NUMBER");
|
||||
do {
|
||||
System.out.println("Enter a number:");
|
||||
try {
|
||||
number = scanner.nextBigInteger();
|
||||
result = new IterativeFactorial().factorial(number);
|
||||
error = false;
|
||||
System.out.println("Factorial of " + number + ": " + result);
|
||||
}
|
||||
catch ( InputMismatchException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
|
||||
catch ( IllegalArgumentException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
}
|
||||
while ( error );
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,56 @@
|
|||
public static long fact(final int n) {
|
||||
if (n < 0){
|
||||
System.err.println("No negative numbers");
|
||||
return 0;
|
||||
package programas;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class RecursiveFactorial {
|
||||
|
||||
public BigInteger factorial(BigInteger n) {
|
||||
if ( n == null ) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return (n < 2) ? 1 : n * fact(n - 1);
|
||||
|
||||
else if ( n.equals(BigInteger.ZERO) ) {
|
||||
return BigInteger.ONE;
|
||||
}
|
||||
else if ( n.signum() == - 1 ) {
|
||||
// negative
|
||||
throw new IllegalArgumentException("Argument must be a non-negative integer");
|
||||
}
|
||||
else {
|
||||
return n.equals(BigInteger.ONE)
|
||||
? BigInteger.ONE
|
||||
: factorial(n.subtract(BigInteger.ONE)).multiply(n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
BigInteger number, result;
|
||||
boolean error = false;
|
||||
System.out.println("FACTORIAL OF A NUMBER");
|
||||
do {
|
||||
System.out.println("Enter a number:");
|
||||
try {
|
||||
number = scanner.nextBigInteger();
|
||||
result = new RecursiveFactorial().factorial(number);
|
||||
error = false;
|
||||
System.out.println("Factorial of " + number + ": " + result);
|
||||
}
|
||||
catch ( InputMismatchException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
|
||||
catch ( IllegalArgumentException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
}
|
||||
while ( error );
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
80
Task/Factorial/LLVM/factorial.llvm
Normal file
80
Task/Factorial/LLVM/factorial.llvm
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
; ModuleID = 'factorial.c'
|
||||
; source_filename = "factorial.c"
|
||||
; target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
|
||||
; target triple = "x86_64-pc-windows-msvc19.21.27702"
|
||||
|
||||
; This is not strictly LLVM, as it uses the C library function "printf".
|
||||
; LLVM does not provide a way to print values, so the alternative would be
|
||||
; to just load the string into memory, and that would be boring.
|
||||
|
||||
; Additional comments have been inserted, as well as changes made from the output produced by clang such as putting more meaningful labels for the jumps
|
||||
|
||||
$"\01??_C@_04PEDNGLFL@?$CFld?6?$AA@" = comdat any
|
||||
|
||||
@"\01??_C@_04PEDNGLFL@?$CFld?6?$AA@" = linkonce_odr unnamed_addr constant [5 x i8] c"%ld\0A\00", comdat, align 1
|
||||
|
||||
;--- The declaration for the external C printf function.
|
||||
declare i32 @printf(i8*, ...)
|
||||
|
||||
; Function Attrs: noinline nounwind optnone uwtable
|
||||
define i32 @factorial(i32) #0 {
|
||||
;-- local copy of n
|
||||
%2 = alloca i32, align 4
|
||||
;-- long result
|
||||
%3 = alloca i32, align 4
|
||||
;-- int i
|
||||
%4 = alloca i32, align 4
|
||||
;-- local n = parameter n
|
||||
store i32 %0, i32* %2, align 4
|
||||
;-- result = 1
|
||||
store i32 1, i32* %3, align 4
|
||||
;-- i = 1
|
||||
store i32 1, i32* %4, align 4
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
;-- i <= n
|
||||
%5 = load i32, i32* %4, align 4
|
||||
%6 = load i32, i32* %2, align 4
|
||||
%7 = icmp sle i32 %5, %6
|
||||
br i1 %7, label %loop_body, label %exit
|
||||
|
||||
loop_body:
|
||||
;-- result *= i
|
||||
%8 = load i32, i32* %4, align 4
|
||||
%9 = load i32, i32* %3, align 4
|
||||
%10 = mul nsw i32 %9, %8
|
||||
store i32 %10, i32* %3, align 4
|
||||
br label %loop_increment
|
||||
|
||||
loop_increment:
|
||||
;-- ++i
|
||||
%11 = load i32, i32* %4, align 4
|
||||
%12 = add nsw i32 %11, 1
|
||||
store i32 %12, i32* %4, align 4
|
||||
br label %loop
|
||||
|
||||
exit:
|
||||
;-- return result
|
||||
%13 = load i32, i32* %3, align 4
|
||||
ret i32 %13
|
||||
}
|
||||
|
||||
; Function Attrs: noinline nounwind optnone uwtable
|
||||
define i32 @main() #0 {
|
||||
;-- factorial(5)
|
||||
%1 = call i32 @factorial(i32 5)
|
||||
;-- printf("%ld\n", factorial(5))
|
||||
%2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @"\01??_C@_04PEDNGLFL@?$CFld?6?$AA@", i32 0, i32 0), i32 %1)
|
||||
;-- return 0
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
||||
|
||||
!llvm.module.flags = !{!0, !1}
|
||||
!llvm.ident = !{!2}
|
||||
|
||||
!0 = !{i32 1, !"wchar_size", i32 2}
|
||||
!1 = !{i32 7, !"PIC Level", i32 2}
|
||||
!2 = !{!"clang version 6.0.1 (tags/RELEASE_601/final)"}
|
||||
|
|
@ -9,5 +9,5 @@ Write $$factorial(1) ; 1
|
|||
Write $$factorial(2) ; 2
|
||||
Write $$factorial(3) ; 6
|
||||
Write $$factorial(10) ; 3628800
|
||||
Write $$factorial(-6) ; Negative numberr
|
||||
Write $$factorial(-6) ; Negative number
|
||||
Write $$factorial(3.7) ; Not an integer
|
||||
|
|
|
|||
1
Task/Factorial/Nickle/factorial.nickle
Normal file
1
Task/Factorial/Nickle/factorial.nickle
Normal file
|
|
@ -0,0 +1 @@
|
|||
int fact(int n) { return n!; }
|
||||
24
Task/Factorial/Phix/factorial-2.phix
Normal file
24
Task/Factorial/Phix/factorial-2.phix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
include mpfr.e
|
||||
mpz f = mpz_init()
|
||||
integer n = 2
|
||||
bool still_running = true,
|
||||
still_printing = true
|
||||
while still_running do
|
||||
atom t0 = time()
|
||||
mpz_fac_ui(f, n)
|
||||
still_running = (time()-t0)<10 -- (stop once over 10s)
|
||||
string ct = elapsed(time()-t0), res, what, pt
|
||||
t0 = time()
|
||||
if still_printing then
|
||||
res = shorten(mpz_get_str(f))
|
||||
what = "printed"
|
||||
still_printing = (time()-t0)<10 -- (stop once over 10s)
|
||||
else
|
||||
res = sprintf("%,d digits",mpz_sizeinbase(f,10))
|
||||
what = "size in base"
|
||||
end if
|
||||
pt = elapsed(time()-t0)
|
||||
printf(1,"factorial(%d):%s, calculated in %s, %s in %s\n",
|
||||
{n,res,ct,what,pt})
|
||||
n *= 2
|
||||
end while
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
from itertools import (accumulate, chain)
|
||||
from operator import mul
|
||||
|
||||
# factorial :: Integer
|
||||
def factorial(n):
|
||||
z=1
|
||||
if n>1:
|
||||
z=n*factorial(n-1)
|
||||
return z
|
||||
return list(
|
||||
accumulate(chain([1], range(1, 1 + n)), mul)
|
||||
)[-1]
|
||||
|
|
|
|||
|
|
@ -1,27 +1,13 @@
|
|||
from cmath import *
|
||||
from itertools import (accumulate, chain)
|
||||
from operator import mul
|
||||
|
||||
# Coefficients used by the GNU Scientific Library
|
||||
g = 7
|
||||
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
|
||||
|
||||
def gamma(z):
|
||||
z = complex(z)
|
||||
# Reflection formula
|
||||
if z.real < 0.5:
|
||||
return pi / (sin(pi*z)*gamma(1-z))
|
||||
else:
|
||||
z -= 1
|
||||
x = p[0]
|
||||
for i in range(1, g+2):
|
||||
x += p[i]/(z+i)
|
||||
t = z + g + 0.5
|
||||
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
|
||||
# factorials :: [Integer]
|
||||
def factorials(n):
|
||||
return list(
|
||||
accumulate(chain([1], range(1, 1 + n)), mul)
|
||||
)
|
||||
|
||||
def factorial(n):
|
||||
return gamma(n+1)
|
||||
print(factorials(5))
|
||||
|
||||
print "factorial(-0.5)**2=",factorial(-0.5)**2
|
||||
for i in range(10):
|
||||
print "factorial(%d)=%s"%(i,factorial(i))
|
||||
# -> [1, 1, 2, 6, 24, 120]
|
||||
|
|
|
|||
5
Task/Factorial/Python/factorial-6.py
Normal file
5
Task/Factorial/Python/factorial-6.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def factorial(n):
|
||||
z=1
|
||||
if n>1:
|
||||
z=n*factorial(n-1)
|
||||
return z
|
||||
27
Task/Factorial/Python/factorial-7.py
Normal file
27
Task/Factorial/Python/factorial-7.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from cmath import *
|
||||
|
||||
# Coefficients used by the GNU Scientific Library
|
||||
g = 7
|
||||
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
|
||||
|
||||
def gamma(z):
|
||||
z = complex(z)
|
||||
# Reflection formula
|
||||
if z.real < 0.5:
|
||||
return pi / (sin(pi*z)*gamma(1-z))
|
||||
else:
|
||||
z -= 1
|
||||
x = p[0]
|
||||
for i in range(1, g+2):
|
||||
x += p[i]/(z+i)
|
||||
t = z + g + 0.5
|
||||
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
|
||||
|
||||
def factorial(n):
|
||||
return gamma(n+1)
|
||||
|
||||
print "factorial(-0.5)**2=",factorial(-0.5)**2
|
||||
for i in range(10):
|
||||
print "factorial(%d)=%s"%(i,factorial(i))
|
||||
2
Task/Factorial/Racket/factorial-3.rkt
Normal file
2
Task/Factorial/Racket/factorial-3.rkt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(define (factorial n)
|
||||
(for/fold ([pro 1]) ([i (in-range 1 (+ n 1))]) (* pro i)))
|
||||
2
Task/Factorial/Racket/factorial-4.rkt
Normal file
2
Task/Factorial/Racket/factorial-4.rkt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(define (factorial n)
|
||||
(for/product ([i (in-range 1 (+ n 1))]) i))
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
def factorial(n: Int)={
|
||||
var res = 1
|
||||
for(i <- 1 to n)
|
||||
res *=i
|
||||
res *= i
|
||||
res
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
def factorial(n: Int) = if(n == 0) 1 else n * factorial(n-1)
|
||||
def factorial(n: Int): Int =
|
||||
if (n == 0) 1
|
||||
else n * factorial(n-1)
|
||||
|
|
|
|||
|
|
@ -1 +1,6 @@
|
|||
def factorial(n: Int) = (2 to n).foldLeft(1)(_*_)
|
||||
def factorial(n: Int) = {
|
||||
@tailrec def fact(x: Int, acc: Int): Int = {
|
||||
if (x < 2) acc else fact(x - 1, acc * x)
|
||||
}
|
||||
fact(n, 1)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1 @@
|
|||
// Note use of big integer support in this version
|
||||
|
||||
implicit def IntToFac(i : Int) = new {
|
||||
def ! = (2 to i).foldLeft(BigInt(1))(_*_)
|
||||
}
|
||||
def factorial(n: Int) = (2 to n).product
|
||||
|
|
|
|||
|
|
@ -1,10 +1,2 @@
|
|||
scala> implicit def IntToFac(i : Int) = new {
|
||||
| def ! = (2 to i).foldLeft(BigInt(1))(_*_)
|
||||
| }
|
||||
IntToFac: (i: Int)java.lang.Object{def !: scala.math.BigInt}
|
||||
|
||||
scala> 20!
|
||||
res0: scala.math.BigInt = 2432902008176640000
|
||||
|
||||
scala> 100!
|
||||
res1: scala.math.BigInt = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||
def factorial(n: Int) =
|
||||
(2 to n).foldLeft(1)(_ * _)
|
||||
|
|
|
|||
3
Task/Factorial/Scala/factorial-6.scala
Normal file
3
Task/Factorial/Scala/factorial-6.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
implicit def IntToFac(i : Int) = new {
|
||||
def ! = (2 to i).foldLeft(BigInt(1))(_ * _)
|
||||
}
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
Number extend [
|
||||
my_factorial [
|
||||
(self < 2) ifTrue: [ ^1 ]
|
||||
ifFalse: [ |c|
|
||||
c := OrderedCollection new.
|
||||
2 to: self do: [ :i | c add: i ].
|
||||
^ (c fold: [ :a :b | a * b ] )
|
||||
]
|
||||
(self < 2)
|
||||
ifTrue: [ ^1 ]
|
||||
ifFalse: [
|
||||
^ (2 to: self) fold: [ :a :b | a * b ]
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
|
|
|
|||
3
Task/Factorial/Smalltalk/factorial-5.st
Normal file
3
Task/Factorial/Smalltalk/factorial-5.st
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fac := [:n | (1 to: n) product].
|
||||
fac value:40
|
||||
-> 815915283247897734345611269596115894272000000000
|
||||
23
Task/Factorial/Spin/factorial.spin
Normal file
23
Task/Factorial/Spin/factorial.spin
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
con
|
||||
_clkmode = xtal1 + pll16x
|
||||
_clkfreq = 80_000_000
|
||||
|
||||
obj
|
||||
ser : "FullDuplexSerial.spin"
|
||||
|
||||
pub main | i
|
||||
ser.start(31, 30, 0, 115200)
|
||||
|
||||
repeat i from 0 to 10
|
||||
ser.dec(fac(i))
|
||||
ser.tx(32)
|
||||
|
||||
waitcnt(_clkfreq + cnt)
|
||||
ser.stop
|
||||
cogstop(0)
|
||||
|
||||
pub fac(n) : f
|
||||
f := 1
|
||||
repeat while n > 0
|
||||
f *= n
|
||||
n -= 1
|
||||
51
Task/Factorial/Visual-Basic-.NET/factorial.visual
Normal file
51
Task/Factorial/Visual-Basic-.NET/factorial.visual
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
Imports System
|
||||
Imports System.Numerics
|
||||
Imports System.Linq
|
||||
|
||||
Module Module1
|
||||
|
||||
' Type Double:
|
||||
|
||||
Function DofactorialI(n As Integer) As Double ' Iterative
|
||||
DofactorialI = 1 : For i As Integer = 1 To n : DofactorialI *= i : Next
|
||||
End Function
|
||||
|
||||
' Type Unsigned Long:
|
||||
|
||||
Function ULfactorialI(n As Integer) As ULong ' Iterative
|
||||
ULfactorialI = 1 : For i As Integer = 1 To n : ULfactorialI *= i : Next
|
||||
End Function
|
||||
|
||||
' Type Decimal:
|
||||
|
||||
Function DefactorialI(n As Integer) As Decimal ' Iterative
|
||||
DefactorialI = 1 : For i As Integer = 1 To n : DefactorialI *= i : Next
|
||||
End Function
|
||||
|
||||
' Extends precision by "dehydrating" and "rehydrating" the powers of ten
|
||||
Function DxfactorialI(n As Integer) As String ' Iterative
|
||||
Dim factorial as Decimal = 1, zeros as integer = 0
|
||||
For i As Integer = 1 To n : factorial *= i
|
||||
If factorial Mod 10 = 0 Then factorial /= 10 : zeros += 1
|
||||
Next : Return factorial.ToString() & New String("0", zeros)
|
||||
End Function
|
||||
|
||||
' Arbitrary Precision:
|
||||
|
||||
Function FactorialI(n As Integer) As BigInteger ' Iterative
|
||||
factorialI = 1 : For i As Integer = 1 To n : factorialI *= i : Next
|
||||
End Function
|
||||
|
||||
Function Factorial(number As Integer) As BigInteger ' Functional
|
||||
Return Enumerable.Range(1, number).Aggregate(New BigInteger(1),
|
||||
Function(acc, num) acc * num)
|
||||
End Function
|
||||
|
||||
Sub Main()
|
||||
Console.WriteLine("Double : {0}! = {1:0}", 20, DoFactorialI(20))
|
||||
Console.WriteLine("ULong : {0}! = {1:0}", 20, ULFactorialI(20))
|
||||
Console.WriteLine("Decimal : {0}! = {1:0}", 27, DeFactorialI(27))
|
||||
Console.WriteLine("Dec.Ext : {0}! = {1:0}", 32, DxFactorialI(32))
|
||||
Console.WriteLine("Arb.Prec: {0}! = {1}", 250, Factorial(250))
|
||||
End Sub
|
||||
End Module
|
||||
26
Task/Factorial/Visual-Basic/factorial.vb
Normal file
26
Task/Factorial/Visual-Basic/factorial.vb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Option Explicit
|
||||
|
||||
Sub Main()
|
||||
Dim i As Variant
|
||||
For i = 1 To 27
|
||||
Debug.Print "Factorial(" & i & ")= , recursive : " & Format$(FactRec(i), "#,###") & " - iterative : " & Format$(FactIter(i), "#,####")
|
||||
Next
|
||||
End Sub 'Main
|
||||
|
||||
Private Function FactRec(n As Variant) As Variant
|
||||
n = CDec(n)
|
||||
If n = 1 Then
|
||||
FactRec = 1#
|
||||
Else
|
||||
FactRec = n * FactRec(n - 1)
|
||||
End If
|
||||
End Function 'FactRec
|
||||
|
||||
Private Function FactIter(n As Variant)
|
||||
Dim i As Variant, f As Variant
|
||||
f = 1#
|
||||
For i = 1# To CDec(n)
|
||||
f = f * i
|
||||
Next i
|
||||
FactIter = f
|
||||
End Function 'FactIter
|
||||
Loading…
Add table
Add a link
Reference in a new issue