Sum digits of an integer: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 793: | Line 793: | ||
print "f0e base 16 : "; SumDigits(0xf0e, 16) |
print "f0e base 16 : "; SumDigits(0xf0e, 16) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
==={{header|M2000 Interpreter}}=== |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
module SumDigitisOfAnInteger { |
|||
z="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|||
sumdigits=lambda z (m as string) ->{ |
|||
integer ret, i |
|||
m=ucase$(m) |
|||
if len(m)=0 then =ret:exit |
|||
for i=1 to len(m):ret+=instr(z, mid$(m,i,1))-1:next |
|||
=ret |
|||
} |
|||
CheckBase=lambda z (m as string, base as integer)->{ |
|||
if len(m)=0 then Error "not valid input" |
|||
if base<2 or base>len(z) then Error "not valid input" |
|||
integer ret=1 |
|||
m=ucase$(m) |
|||
for i=1 to len(m) |
|||
ret*=instr(z, mid$(m,i,1))<=base |
|||
if ret=0 then exit for |
|||
next |
|||
=ret<>0 |
|||
} |
|||
string n |
|||
integer b |
|||
stack new { |
|||
data "1", 10 |
|||
data "1234", 10 |
|||
data ""+0xfe, 10 |
|||
data "fe", 16 |
|||
data "f0e", 16 |
|||
while not empty |
|||
read n, b |
|||
Print n+" (base:"+b+") sums to "+sumdigits(n) |
|||
end while |
|||
} |
|||
Input "number, base :", n, b |
|||
if CheckBase(n, b) then |
|||
Print "sums to "+sumdigits(n) |
|||
else |
|||
Print n;" isn't a number of base "+b |
|||
end if |
|||
} |
|||
SumDigitisOfAnInteger |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 (base:10) sums to 1 |
|||
1234 (base:10) sums to 10 |
|||
254 (base:10) sums to 11 |
|||
fe (base:16) sums to 29 |
|||
f0e (base:16) sums to 29 |
|||
number, base :12345671234567, 8 |
|||
sums to 56 |
|||
</pre> |
|||
==={{header|Minimal BASIC}}=== |
==={{header|Minimal BASIC}}=== |
||
Line 2,563: | Line 2,506: | ||
29 |
29 |
||
29</pre> |
29</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
module SumDigitisOfAnInteger { |
|||
z="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|||
sumdigits=lambda z (m as string) ->{ |
|||
integer ret, i |
|||
m=ucase$(m) |
|||
if len(m)=0 then =ret:exit |
|||
for i=1 to len(m):ret+=instr(z, mid$(m,i,1))-1:next |
|||
=ret |
|||
} |
|||
CheckBase=lambda z (m as string, base as integer)->{ |
|||
if len(m)=0 then Error "not valid input" |
|||
if base<2 or base>len(z) then Error "not valid input" |
|||
integer ret=1 |
|||
m=ucase$(m) |
|||
for i=1 to len(m) |
|||
ret*=instr(z, mid$(m,i,1))<=base |
|||
if ret=0 then exit for |
|||
next |
|||
=ret<>0 |
|||
} |
|||
string n |
|||
integer b |
|||
stack new { |
|||
data "1", 10 |
|||
data "1234", 10 |
|||
data ""+0xfe, 10 |
|||
data "fe", 16 |
|||
data "f0e", 16 |
|||
while not empty |
|||
read n, b |
|||
Print n+" (base:"+b+") sums to "+sumdigits(n) |
|||
end while |
|||
} |
|||
Input "number, base :", n, b |
|||
if CheckBase(n, b) then |
|||
Print "sums to "+sumdigits(n) |
|||
else |
|||
Print n;" isn't a number of base "+b |
|||
end if |
|||
} |
|||
SumDigitisOfAnInteger |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 (base:10) sums to 1 |
|||
1234 (base:10) sums to 10 |
|||
254 (base:10) sums to 11 |
|||
fe (base:16) sums to 29 |
|||
f0e (base:16) sums to 29 |
|||
number, base :12345671234567, 8 |
|||
sums to 56 |
|||
</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |