constant Rate { value: 50 }
signal N { default: 0 rate: Rate }
signal FactorialResult { default: 0 rate: Rate }
N >> Factorial () >> FactorialResult;
module Factorial {
input: Input
output: Output
propertyName: none
propertyBlock: none
internalBlock: [
signal Input { default: 0 rate: none },
signal Output { default: 0 rate: none },
signal Result { default: 0 rate: none reset: Reset },
signal Count { default: Input rate: none reset: Reset },
switch End { default: off reset: Reset },
switch FlipFlop { default: off reset: none },
trigger Reset {},
reaction FactorialReaction {
output: Result
onExecution: Reset
terminateWhen: Done
propertyName: 'value'
propertyBlock: signal Value { default: 0 }
internalBlock: [
signal Result { default: 0 rate: none },
switch Done { default: off reset: Reset },
trigger Reset {}
]
streams: [
Value >> Factorial () >> Result;
on >> Done;
]
}
]
streams: [
FlipFlop >> Select ( whenOn: off whenOff: on ) >> FlipFlop >> OnEdge (edge: 'Both' ) >> Reset;
Count >> Compare ( value: 0 operator: 'Equal' ) >> End;
not End >> FactorialReaction ( value: Count - 1 ) >> Result;
End >> Select ( whenOn: 1 whenOff: Count * Result ) >> Output;
]
}
Python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
C / C++
unsigned int factorial (unsigned int f) {
if (f == 0) {
return 1;
} else {
return f * factorial(f - 1);
}
}