langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
51
Task/Factorial/NetRexx/factorial.netrexx
Normal file
51
Task/Factorial/NetRexx/factorial.netrexx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* NetRexx */
|
||||
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
numeric digits 64 -- switch to exponential format when numbers become larger than 64 digits
|
||||
|
||||
say 'Input a number: \-'
|
||||
say
|
||||
do
|
||||
n_ = long ask -- Gets the number, must be an integer
|
||||
|
||||
say n_'! =' factorial(n_) '(using iteration)'
|
||||
say n_'! =' factorial(n_, 'r') '(using recursion)'
|
||||
|
||||
catch ex = Exception
|
||||
ex.printStackTrace
|
||||
end
|
||||
|
||||
return
|
||||
|
||||
method factorial(n_ = long, fmethod = 'I') public static returns Rexx signals IllegalArgumentException
|
||||
|
||||
if n_ < 0 then -
|
||||
signal IllegalArgumentException('Sorry, but' n_ 'is not a positive integer')
|
||||
|
||||
select
|
||||
when fmethod.upper = 'R' then -
|
||||
fact = factorialRecursive(n_)
|
||||
otherwise -
|
||||
fact = factorialIterative(n_)
|
||||
end
|
||||
|
||||
return fact
|
||||
|
||||
method factorialIterative(n_ = long) private static returns Rexx
|
||||
|
||||
fact = 1
|
||||
loop i_ = 1 to n_
|
||||
fact = fact * i_
|
||||
end i_
|
||||
|
||||
return fact
|
||||
|
||||
method factorialRecursive(n_ = long) private static returns Rexx
|
||||
|
||||
if n_ > 1 then -
|
||||
fact = n_ * factorialRecursive(n_ - 1)
|
||||
else -
|
||||
fact = 1
|
||||
|
||||
return fact
|
||||
Loading…
Add table
Add a link
Reference in a new issue