17 lines
401 B
Plaintext
17 lines
401 B
Plaintext
main( argv_ ) {
|
|
if ( size( argv_ ) < 2 ) {
|
|
throw Exception( "usage: digital-root {NUM}" );
|
|
}
|
|
n = argv_[1];
|
|
if ( ( size( n ) == 0 ) || ( n.find_other_than( "0123456789" ) >= 0 ) ) {
|
|
throw Exception( "{} is not a number".format( n ) );
|
|
}
|
|
shift = integer( '0' ) + 1;
|
|
acc = 0;
|
|
for ( d : n ) {
|
|
acc = 1 + ( acc + integer( d ) - shift ) % 9;
|
|
}
|
|
print( "{}\n".format( acc ) );
|
|
return ( 0 );
|
|
}
|