Solve hanging lantern problem: Difference between revisions

Solve hanging lantern problem in various BASIC dialents (BASIC256, QBasic, PureBasic and Yabasic)
(Solve hanging lantern problem in FreeBASIC)
(Solve hanging lantern problem in various BASIC dialents (BASIC256, QBasic, PureBasic and Yabasic))
Line 44:
[b,a,c,…]
……
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
The result for n >= 5 is slow to emerge
<lang freebasic>arraybase 1
n = 4
dim a(n)
for i = 1 to a[?]
a[i] = i
print "[ ";
for j = 1 to i
print a[j]; " ";
next j
print "] = "; getLantern(a)
next i
end
 
function getLantern(arr)
res = 0
for i = 1 to arr[?]
if arr[i] <> 0 then
arr[i] -= 1
res += getLantern(arr)
arr[i] += 1
end if
next i
if res = 0 then res = 1
return res
end function</lang>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
The result for n >= 5 is slow to emerge
<lang QBasic>FUNCTION getLantern (arr())
res = 0
FOR i = 1 TO UBOUND(arr)
IF arr(i) <> 0 THEN
arr(i) = arr(i) - 1
res = res + getLantern(arr())
arr(i) = arr(i) + 1
END IF
NEXT i
IF res = 0 THEN res = 1
getLantern = res
END FUNCTION
 
n = 4
DIM a(n)
FOR i = 1 TO UBOUND(a)
a(i) = i
PRINT "[";
FOR j = 1 TO i
PRINT a(j); " ";
NEXT j
PRINT "] = "; getLantern(a())
NEXT i
END</lang>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
The result for n >= 5 is slow to emerge
<lang PureBasic>;;The result For n >= 5 is slow To emerge
Procedure getLantern(Array arr(1))
res.l = 0
For i.l = 1 To ArraySize(arr(),1)
If arr(i) <> 0
arr(i) - 1
res + getLantern(arr())
arr(i) + 1
EndIf
Next i
If res = 0
res = 1
EndIf
ProcedureReturn res
EndProcedure
 
OpenConsole()
n.i = 4
Dim a.i(n)
For i.l = 1 To ArraySize(a())
a(i) = i
Print("[")
For j.l = 1 To i
Print(Str(a(j)) + " ")
Next j
PrintN("] = " + Str(getLantern(a())))
Next i
Input()
CloseConsole()</lang>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
The result for n >= 5 is slow to emerge
<lang yabasic>n = 4
dim a(n)
for i = 1 to arraysize(a(),1)
a(i) = i
print "[ ";
for j = 1 to i
print a(j), " ";
next j
print "] = ", getLantern(a())
next i
 
sub getLantern(arr())
local res, i
res = 0
for i = 1 to arraysize(arr(),1)
if arr(i) <> 0 then
arr(i) = arr(i) - 1
res = res + getLantern(arr())
arr(i) = arr(i) + 1
fi
next i
if res = 0 res = 1
return res
end sub</lang>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
 
Line 112 ⟶ 242:
Print "] = "; getLantern(a())
Next i
'
Color 3 : Print !"\n--- terminado, pulsa RETURN---"
Sleep</lang>
{{out}}
2,136

edits