How to resolve the algorithm Sum multiples of 3 and 5 step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum multiples of 3 and 5 step by step in the BASIC programming language
Table of Contents
Problem Statement
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000. This is is the same as Project Euler problem 1. Extra credit: do this efficiently for n = 1e20 or higher.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the BASIC programming language
Source code in the basic programming language
Declare function mulsum35(n as integer) as integer
Function mulsum35(n as integer) as integer
Dim s as integer
For i as integer = 1 to n - 1
If (i mod 3 = 0) or (i mod 5 = 0) then
s += i
End if
Next i
Return s
End Function
Print mulsum35(1000)
Sleep
End
10 INPUT N
20 LET SUM = 0
30 FOR I = 3 TO N - 1
40 IF I / 3 = INT (I / 3) OR I / 5 = INT (I / 5) THEN SUM = SUM + I
50 NEXT I
60 PRINT SUM
function multSum35(n)
if n = 0 then return 0
suma = 0
for i = 1 to n
if (i mod 3 = 0) or (i mod 5 = 0) then suma += i
next i
return suma
end function
print multSum35(999)
end
10 cls
20 print multsum35(1000)
30 end
40 function multsum35(n)
50 suma = 0
60 for i = 1 to n-1
70 if (i mod 3 = 0) or (i mod 5 = 0) then suma = suma+i
80 next i
90 multsum35 = suma
100 end function
Public Sub Main()
Print "Sum of positive integers below 1000 divisible by 3 or 5 is : "; multSum35(999)
End
Function multSum35(n As Integer) As Integer
If n = 0 Then Return 0
Dim suma As Integer = 0
For i As Integer = 1 To n
If (i Mod 3 = 0) Or (i Mod 5 = 0) Then suma += i
Next
Return suma
End Function
10 CLS : REM 10 HOME for Applesoft BASIC
20 LET N = 1000
30 GOSUB 60
40 PRINT S
50 END
60 REM multsum35
70 LET S = 0
80 FOR I = 1 TO N-1
90 IF I/3 = INT(I/3) OR I/5 = INT(I/5) THEN LET S = S+I : REM for Applesoft BASIC & Quite BASIC
90 IF (I MOD 3 = 0) OR (I MOD 5 = 0) THEN LET S = S+I : REM for MSX Basic & Chipmunk Basic
100 NEXT I
110 RETURN
10 INPUT N
20 LET S = 0
30 FOR I = 3 TO N - 1
40 IF I / 3 = INT (I / 3) THEN 70
50 IF I / 5 = INT (I / 5) THEN 70
60 GOTO 80
70 LET S = S + I
80 NEXT I
90 PRINT S
100 END
FUNCTION multSum35 (n)
IF n = 0 THEN multSum35 = 0
suma = 0
FOR i = 1 TO n
IF (i MOD 3 = 0) OR (i MOD 5 = 0) THEN suma = suma + i
NEXT i
multSum35 = suma
END FUNCTION
PRINT multSum35(999)
FUNCTION multSum35(n)
IF n = 0 THEN LET multSum35 = 0
LET suma = 0
FOR i = 1 TO n
IF MOD(i, 3) = 0 OR MOD(i, 5) = 0 THEN LET suma = suma + i
NEXT i
LET multSum35 = suma
END FUNCTION
PRINT multSum35(999)
END
sub multSum35(n)
if n = 0 then return 0 : fi
suma = 0
for i = 1 to n
if mod(i, 3) = 0 or mod(i, 5) = 0 then suma = suma + i : fi
next i
return suma
end sub
print multSum35(999)
end
100 PRINT MULTSUM35(1000)
110 DEF MULTSUM35(N)
120 LET S=0
130 FOR I=1 TO N-1
140 IF MOD(I,3)=0 OR MOD(I,5)=0 THEN LET S=S+I
150 NEXT
160 LET MULTSUM35=S
170 END DEF
10 INPUT N
20 FAST
30 LET SUM=0
40 FOR I=3 TO N-1
50 IF I/3=INT (I/3) THEN GOTO 70
60 IF I/5<>INT (I/5) THEN GOTO 80
70 LET SUM=SUM+I
80 NEXT I
90 SLOW
100 PRINT SUM
You may also check:How to resolve the algorithm Variable-length quantity step by step in the xTalk programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the SSEM programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Kotlin programming language