Sum digits of an integer: Difference between revisions
Content added Content deleted
(→{{header|Lambdatalk}}: adding lambdatalk task) |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 793: | Line 793: | ||
print "f0e base 16 : "; SumDigits(0xf0e, 16) |
print "f0e base 16 : "; SumDigits(0xf0e, 16) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
==={{header|BBC BASIC}}=== |
|||
{{works with|BBC BASIC for Windows}} |
|||
This solution deliberately avoids MOD and DIV so it is not restricted to 32-bit integers. |
|||
<syntaxhighlight lang="bbcbasic"> *FLOAT64 |
|||
PRINT "Digit sum of 1 (base 10) is "; FNdigitsum(1, 10) |
|||
PRINT "Digit sum of 12345 (base 10) is "; FNdigitsum(12345, 10) |
|||
PRINT "Digit sum of 9876543210 (base 10) is "; FNdigitsum(9876543210, 10) |
|||
PRINT "Digit sum of FE (base 16) is "; ~FNdigitsum(&FE, 16) " (base 16)" |
|||
PRINT "Digit sum of F0E (base 16) is "; ~FNdigitsum(&F0E, 16) " (base 16)" |
|||
END |
|||
DEF FNdigitsum(n, b) |
|||
LOCAL q, s |
|||
WHILE n <> 0 |
|||
q = INT(n / b) |
|||
s += n - q * b |
|||
n = q |
|||
ENDWHILE |
|||
= s</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Digit sum of 1 (base 10) is 1 |
|||
Digit sum of 12345 (base 10) is 15 |
|||
Digit sum of 9876543210 (base 10) is 45 |
|||
Digit sum of FE (base 16) is 1D (base 16) |
|||
Digit sum of F0E (base 16) is 1D (base 16) |
|||
</pre> |
|||
==={{header|Chipmunk Basic}}=== |
==={{header|Chipmunk Basic}}=== |
||
Line 816: | Line 844: | ||
180 sumdigits = sum |
180 sumdigits = sum |
||
190 return</syntaxhighlight> |
190 return</syntaxhighlight> |
||
==={{header|FreeBASIC}}=== |
|||
{{trans|PureBasic}} |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Function SumDigits(number As Integer, nBase As Integer) As Integer |
|||
If number < 0 Then number = -number ' convert negative numbers to positive |
|||
If nBase < 2 Then nBase = 2 ' nBase can't be less than 2 |
|||
Dim As Integer sum = 0 |
|||
While number > 0 |
|||
sum += number Mod nBase |
|||
number \= nBase |
|||
Wend |
|||
Return sum |
|||
End Function |
|||
Print "The sums of the digits are:" |
|||
Print |
|||
Print "1 base 10 :"; SumDigits(1, 10) |
|||
Print "1234 base 10 :"; SumDigits(1234, 10) |
|||
Print "fe base 16 :"; SumDigits(&Hfe, 16) |
|||
Print "f0e base 16 :"; SumDigits(&Hf0e, 16) |
|||
Print |
|||
Print "Press any key to quit the program" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The sums of the digits are: |
|||
1 base 10 : 1 |
|||
1234 base 10 : 10 |
|||
fe base 16 : 29 |
|||
f0e base 16 : 29 |
|||
</pre> |
|||
==={{header|GW-BASIC}}=== |
==={{header|GW-BASIC}}=== |
||
Line 859: | Line 922: | ||
110 END |
110 END |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic"> |
|||
EnableExplicit |
|||
Procedure.i SumDigits(Number.q, Base) |
|||
If Number < 0 : Number = -Number : EndIf; convert negative numbers to positive |
|||
If Base < 2 : Base = 2 : EndIf ; base can't be less than 2 |
|||
Protected sum = 0 |
|||
While Number > 0 |
|||
sum + Number % Base |
|||
Number / Base |
|||
Wend |
|||
ProcedureReturn sum |
|||
EndProcedure |
|||
If OpenConsole() |
|||
PrintN("The sums of the digits are:") |
|||
PrintN("") |
|||
PrintN("1 base 10 : " + SumDigits(1, 10)) |
|||
PrintN("1234 base 10 : " + SumDigits(1234, 10)) |
|||
PrintN("fe base 16 : " + SumDigits($fe, 16)) |
|||
PrintN("f0e base 16 : " + SumDigits($f0e, 16)) |
|||
PrintN("") |
|||
PrintN("Press any key to close the console") |
|||
Repeat: Delay(10) : Until Inkey() <> "" |
|||
CloseConsole() |
|||
EndIf |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The sums of the digits are: |
|||
1 base 10 : 1 |
|||
1234 base 10 : 10 |
|||
fe base 16 : 29 |
|||
f0e base 16 : 29 |
|||
</pre> |
|||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
Line 883: | Line 985: | ||
PRINT "f0e base 16 :"; SumDigits(&HF0E, 16) |
PRINT "f0e base 16 :"; SumDigits(&HF0E, 16) |
||
END</syntaxhighlight> |
END</syntaxhighlight> |
||
==={{header|QuickBASIC}}=== |
|||
{{works with|QBasic}} |
|||
{{works with|QB64}} |
|||
{{works with|VB-DOS}} |
|||
<syntaxhighlight lang="qbasic"> |
|||
DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER) |
|||
CLS |
|||
PRINT "1 base 10 ->"; SumDigits%(1, 10) |
|||
PRINT "1234 base 10 ->"; SumDigits%(1234, 10) |
|||
PRINT "FE base 16 ->"; SumDigits%(&HFE, 16); " (Hex -> "; HEX$(SumDigits%(&HFE, 16)); ")" |
|||
PRINT "F0E base 16 ->"; SumDigits%(&HF0E, 16); " (Hex -> "; HEX$(SumDigits%(&HF0E, 16)); ")" |
|||
FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER) |
|||
' Var |
|||
DIM iSum AS INTEGER |
|||
Num = ABS(Num) ' Should be a positive number |
|||
IF NBase < 2 THEN NBase = 10 ' Default decimal |
|||
DO WHILE Num > 0 |
|||
iSum = iSum + (Num MOD NBase) |
|||
Num = Num \ NBase |
|||
LOOP |
|||
SumDigits% = iSum |
|||
END FUNCTION |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 base 10 -> 1 |
|||
1234 base 10 -> 10 |
|||
FE base 16 -> 11 |
|||
F0E base 16 -> 20 |
|||
</pre> |
|||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
Line 905: | Line 1,042: | ||
{{out}} |
{{out}} |
||
<pre>Same as BASIC256 entry.</pre> |
<pre>Same as BASIC256 entry.</pre> |
||
==={{header|TI-83 BASIC}}=== |
|||
<syntaxhighlight lang="ti-83b">"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"→Str1 |
|||
Disp "SUM DIGITS OF INT" |
|||
Disp "-----------------" |
|||
Disp "ENTER NUMBER" |
|||
Input Str2 |
|||
Disp "ENTER BASE" |
|||
Input B |
|||
0→R |
|||
length(Str2)→L |
|||
For(I,1,L,1) |
|||
sub(Str2,I,1)→Str3 |
|||
inString(Str1,Str3)-1→S |
|||
If S≥B or S=-1:Then |
|||
Disp "ERROR:" |
|||
Disp Str3 |
|||
Disp "NOT IN BASE" |
|||
Disp B |
|||
Stop |
|||
End |
|||
R+S→R |
|||
End |
|||
Disp R</syntaxhighlight> |
|||
==={{header|Tiny BASIC}}=== |
|||
Only base ten is supported because the only data type is signed 16-bit int. |
|||
<syntaxhighlight lang="tinybasic"> |
|||
PRINT "Enter a number." |
|||
INPUT N |
|||
IF N < 0 THEN LET N = -N |
|||
LET S = 0 |
|||
10 IF N = 0 THEN GOTO 20 |
|||
LET S = S + N - 10*(N/10) |
|||
LET N = N / 10 |
|||
GOTO 10 |
|||
20 PRINT "Its digit sum is ",S,"." |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Enter a number. |
|||
-11212 |
|||
Its digit sum is 7.</pre> |
|||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
Line 927: | Line 1,106: | ||
PRINT "f0e base 16 :"; SumDigits(3854, 16) !0xf0e |
PRINT "f0e base 16 :"; SumDigits(3854, 16) !0xf0e |
||
END</syntaxhighlight> |
END</syntaxhighlight> |
||
==={{header|Visual Basic}}=== |
|||
This version checks that only valid digits for the indicated base are passed in, exiting otherwise. |
|||
<syntaxhighlight lang="vb">Function sumDigits(num As Variant, base As Long) As Long |
|||
'can handle up to base 36 |
|||
Dim outp As Long |
|||
Dim validNums As String, tmp As Variant, x As Long, lennum As Long |
|||
'ensure num contains only valid characters |
|||
validNums = Left$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", base) |
|||
lennum = Len(num) |
|||
For L0 = lennum To 1 Step -1 |
|||
x = InStr(validNums, Mid$(num, L0, 1)) - 1 |
|||
If -1 = x Then Exit Function |
|||
tmp = tmp + (x * (base ^ (lennum - L0))) |
|||
Next |
|||
While tmp |
|||
outp = outp + (tmp Mod base) |
|||
tmp = tmp \ base |
|||
Wend |
|||
sumDigits = outp |
|||
End Function |
|||
Sub tester() |
|||
Debug.Print sumDigits(1, 10) |
|||
Debug.Print sumDigits(1234, 10) |
|||
Debug.Print sumDigits(&HFE, 16) |
|||
Debug.Print sumDigits(&HF0E, 16) |
|||
Debug.Print sumDigits("2", 2) |
|||
End Sub</syntaxhighlight> |
|||
{{out}} (in the debug window): |
|||
<pre> |
|||
1 |
|||
10 |
|||
11 |
|||
20 |
|||
0 |
|||
</pre> |
|||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
Line 946: | Line 1,166: | ||
print "f0e base 16 : ", SumDigits(0xf0e, 16) |
print "f0e base 16 : ", SumDigits(0xf0e, 16) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
==={{header|QuickBASIC}}=== |
|||
{{works with|QBasic}} |
|||
{{works with|QB64}} |
|||
{{works with|VB-DOS}} |
|||
<syntaxhighlight lang="qbasic"> |
|||
DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER) |
|||
CLS |
|||
PRINT "1 base 10 ->"; SumDigits%(1, 10) |
|||
PRINT "1234 base 10 ->"; SumDigits%(1234, 10) |
|||
PRINT "FE base 16 ->"; SumDigits%(&HFE, 16); " (Hex -> "; HEX$(SumDigits%(&HFE, 16)); ")" |
|||
PRINT "F0E base 16 ->"; SumDigits%(&HF0E, 16); " (Hex -> "; HEX$(SumDigits%(&HF0E, 16)); ")" |
|||
FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER) |
|||
' Var |
|||
DIM iSum AS INTEGER |
|||
Num = ABS(Num) ' Should be a positive number |
|||
IF NBase < 2 THEN NBase = 10 ' Default decimal |
|||
DO WHILE Num > 0 |
|||
iSum = iSum + (Num MOD NBase) |
|||
Num = Num \ NBase |
|||
LOOP |
|||
SumDigits% = iSum |
|||
END FUNCTION |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 base 10 -> 1 |
|||
1234 base 10 -> 10 |
|||
FE base 16 -> 11 |
|||
F0E base 16 -> 20 |
|||
</pre> |
|||
==={{header|Tiny BASIC}}=== |
|||
Only base ten is supported because the only data type is signed 16-bit int. |
|||
<syntaxhighlight lang="tinybasic"> |
|||
PRINT "Enter a number." |
|||
INPUT N |
|||
IF N < 0 THEN LET N = -N |
|||
LET S = 0 |
|||
10 IF N = 0 THEN GOTO 20 |
|||
LET S = S + N - 10*(N/10) |
|||
LET N = N / 10 |
|||
GOTO 10 |
|||
20 PRINT "Its digit sum is ",S,"." |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Enter a number. |
|||
-11212 |
|||
Its digit sum is 7.</pre> |
|||
=={{header|BBC BASIC}}== |
|||
{{works with|BBC BASIC for Windows}} |
|||
This solution deliberately avoids MOD and DIV so it is not restricted to 32-bit integers. |
|||
<syntaxhighlight lang="bbcbasic"> *FLOAT64 |
|||
PRINT "Digit sum of 1 (base 10) is "; FNdigitsum(1, 10) |
|||
PRINT "Digit sum of 12345 (base 10) is "; FNdigitsum(12345, 10) |
|||
PRINT "Digit sum of 9876543210 (base 10) is "; FNdigitsum(9876543210, 10) |
|||
PRINT "Digit sum of FE (base 16) is "; ~FNdigitsum(&FE, 16) " (base 16)" |
|||
PRINT "Digit sum of F0E (base 16) is "; ~FNdigitsum(&F0E, 16) " (base 16)" |
|||
END |
|||
DEF FNdigitsum(n, b) |
|||
LOCAL q, s |
|||
WHILE n <> 0 |
|||
q = INT(n / b) |
|||
s += n - q * b |
|||
n = q |
|||
ENDWHILE |
|||
= s</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Digit sum of 1 (base 10) is 1 |
|||
Digit sum of 12345 (base 10) is 15 |
|||
Digit sum of 9876543210 (base 10) is 45 |
|||
Digit sum of FE (base 16) is 1D (base 16) |
|||
Digit sum of F0E (base 16) is 1D (base 16) |
|||
</pre> |
|||
=={{header|bc}}== |
=={{header|bc}}== |
||
Line 1,930: | Line 2,069: | ||
end program digit_sum |
end program digit_sum |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|PureBasic}} |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Function SumDigits(number As Integer, nBase As Integer) As Integer |
|||
If number < 0 Then number = -number ' convert negative numbers to positive |
|||
If nBase < 2 Then nBase = 2 ' nBase can't be less than 2 |
|||
Dim As Integer sum = 0 |
|||
While number > 0 |
|||
sum += number Mod nBase |
|||
number \= nBase |
|||
Wend |
|||
Return sum |
|||
End Function |
|||
Print "The sums of the digits are:" |
|||
Print |
|||
Print "1 base 10 :"; SumDigits(1, 10) |
|||
Print "1234 base 10 :"; SumDigits(1234, 10) |
|||
Print "fe base 16 :"; SumDigits(&Hfe, 16) |
|||
Print "f0e base 16 :"; SumDigits(&Hf0e, 16) |
|||
Print |
|||
Print "Press any key to quit the program" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The sums of the digits are: |
|||
1 base 10 : 1 |
|||
1234 base 10 : 10 |
|||
fe base 16 : 29 |
|||
f0e base 16 : 29 |
|||
</pre> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Line 3,625: | Line 3,729: | ||
Sum of digits of 254 in base 16 is 29. |
Sum of digits of 254 in base 16 is 29. |
||
Sum of digits of 3854 in base 16 is 29. |
Sum of digits of 3854 in base 16 is 29. |
||
</pre> |
|||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic"> |
|||
EnableExplicit |
|||
Procedure.i SumDigits(Number.q, Base) |
|||
If Number < 0 : Number = -Number : EndIf; convert negative numbers to positive |
|||
If Base < 2 : Base = 2 : EndIf ; base can't be less than 2 |
|||
Protected sum = 0 |
|||
While Number > 0 |
|||
sum + Number % Base |
|||
Number / Base |
|||
Wend |
|||
ProcedureReturn sum |
|||
EndProcedure |
|||
If OpenConsole() |
|||
PrintN("The sums of the digits are:") |
|||
PrintN("") |
|||
PrintN("1 base 10 : " + SumDigits(1, 10)) |
|||
PrintN("1234 base 10 : " + SumDigits(1234, 10)) |
|||
PrintN("fe base 16 : " + SumDigits($fe, 16)) |
|||
PrintN("f0e base 16 : " + SumDigits($f0e, 16)) |
|||
PrintN("") |
|||
PrintN("Press any key to close the console") |
|||
Repeat: Delay(10) : Until Inkey() <> "" |
|||
CloseConsole() |
|||
EndIf |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The sums of the digits are: |
|||
1 base 10 : 1 |
|||
1234 base 10 : 10 |
|||
fe base 16 : 29 |
|||
f0e base 16 : 29 |
|||
</pre> |
</pre> |
||
Line 4,368: | Line 4,433: | ||
162 |
162 |
||
</pre> |
</pre> |
||
=={{header|TI-83 BASIC}}== |
|||
<syntaxhighlight lang="ti-83b">"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"→Str1 |
|||
Disp "SUM DIGITS OF INT" |
|||
Disp "-----------------" |
|||
Disp "ENTER NUMBER" |
|||
Input Str2 |
|||
Disp "ENTER BASE" |
|||
Input B |
|||
0→R |
|||
length(Str2)→L |
|||
For(I,1,L,1) |
|||
sub(Str2,I,1)→Str3 |
|||
inString(Str1,Str3)-1→S |
|||
If S≥B or S=-1:Then |
|||
Disp "ERROR:" |
|||
Disp Str3 |
|||
Disp "NOT IN BASE" |
|||
Disp B |
|||
Stop |
|||
End |
|||
R+S→R |
|||
End |
|||
Disp R</syntaxhighlight> |
|||
=={{header|Transd}}== |
=={{header|Transd}}== |
||
Line 4,477: | Line 4,517: | ||
29 |
29 |
||
29</pre> |
29</pre> |
||
=={{header|Visual Basic}}== |
|||
This version checks that only valid digits for the indicated base are passed in, exiting otherwise. |
|||
<syntaxhighlight lang="vb">Function sumDigits(num As Variant, base As Long) As Long |
|||
'can handle up to base 36 |
|||
Dim outp As Long |
|||
Dim validNums As String, tmp As Variant, x As Long, lennum As Long |
|||
'ensure num contains only valid characters |
|||
validNums = Left$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", base) |
|||
lennum = Len(num) |
|||
For L0 = lennum To 1 Step -1 |
|||
x = InStr(validNums, Mid$(num, L0, 1)) - 1 |
|||
If -1 = x Then Exit Function |
|||
tmp = tmp + (x * (base ^ (lennum - L0))) |
|||
Next |
|||
While tmp |
|||
outp = outp + (tmp Mod base) |
|||
tmp = tmp \ base |
|||
Wend |
|||
sumDigits = outp |
|||
End Function |
|||
Sub tester() |
|||
Debug.Print sumDigits(1, 10) |
|||
Debug.Print sumDigits(1234, 10) |
|||
Debug.Print sumDigits(&HFE, 16) |
|||
Debug.Print sumDigits(&HF0E, 16) |
|||
Debug.Print sumDigits("2", 2) |
|||
End Sub</syntaxhighlight> |
|||
{{out}} (in the debug window): |
|||
<pre> |
|||
1 |
|||
10 |
|||
11 |
|||
20 |
|||
0 |
|||
</pre> |
|||
=={{header|V (Vlang)}}== |
=={{header|V (Vlang)}}== |