29 lines
781 B
Text
29 lines
781 B
Text
#!/usr/local/bin/spar
|
|
pragma annotate( summary, "factorial n" )
|
|
@( description, "Write a function to return the factorial of a number." )
|
|
@( author, "Ken O. Burtch" );
|
|
pragma license( unrestricted );
|
|
|
|
pragma restriction( no_external_commands );
|
|
|
|
procedure factorial is
|
|
fact_pos : constant integer := numerics.value( $1 );
|
|
result : natural;
|
|
count : natural;
|
|
begin
|
|
if fact_pos < 0 then
|
|
put_line( standard_error, source_info.source_location & ": number must be >= 0" );
|
|
command_line.set_exit_status( 192 );
|
|
return;
|
|
end if;
|
|
if fact_pos = 0 then
|
|
? 0;
|
|
return;
|
|
end if;
|
|
result := natural( fact_pos );
|
|
count := natural( fact_pos - 1 );
|
|
for i in reverse 1..count loop
|
|
result := @ * i;
|
|
end loop;
|
|
? result;
|
|
end factorial;
|