First 9 prime Fibonacci number: Difference between revisions
Content deleted Content added
Catskill549 (talk | contribs) added AWK |
First 9 Prime Fibonacci Number in various BASIC dialents |
||
Line 64: | Line 64: | ||
2 3 5 13 89 233 1597 28657 514229 |
2 3 5 13 89 233 1597 28657 514229 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
==={{header|BASIC256}}=== |
|||
<lang BASIC256>function isPrime(v) |
|||
if v < 2 then return False |
|||
if v mod 2 = 0 then return v = 2 |
|||
if v mod 3 = 0 then return v = 3 |
|||
d = 5 |
|||
while d * d <= v |
|||
if v mod d = 0 then return False else d += 2 |
|||
end while |
|||
return True |
|||
end function |
|||
function fib(nr) |
|||
if nr = 0 then return 0 |
|||
if nr = 1 then return 1 |
|||
if nr > 1 then return fib(nr-1) + fib(nr-2) |
|||
end function |
|||
i = 0 |
|||
cont = 0 |
|||
print "The first 9 Prime Fibonacci numbers: " |
|||
while True |
|||
i += 1 |
|||
num = fib(i) |
|||
if isPrime(num) then |
|||
cont += 1 |
|||
if cont < 10 then |
|||
print num; " "; |
|||
else |
|||
exit while |
|||
end if |
|||
end if |
|||
end while |
|||
end</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<lang freebasic>Function isPrime(Byval ValorEval As Integer) As Boolean |
|||
If ValorEval <= 1 Then Return False |
|||
For i As Integer = 2 To Int(Sqr(ValorEval)) |
|||
If ValorEval Mod i = 0 Then Return False |
|||
Next i |
|||
Return True |
|||
End Function |
|||
Function fib(nr As Integer) As Integer |
|||
If nr = 0 Then Return 0 |
|||
If nr = 1 Then Return 1 |
|||
If nr > 1 Then Return fib(nr-1) + fib(nr-2) |
|||
End Function |
|||
Dim As Integer i = 0, num, cont = 0 |
|||
Print "The first 9 Prime Fibonacci numbers: " |
|||
Do |
|||
i += 1 |
|||
num = fib(i) |
|||
If isprime(num) Then |
|||
cont += 1 |
|||
If cont < 10 Then |
|||
Print num; " "; |
|||
Else |
|||
Exit Do |
|||
End If |
|||
End If |
|||
Loop |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre>The first 9 Prime Fibonacci numbers: |
|||
2 3 5 13 89 233 1597 28657 514229</pre> |
|||
==={{header|PureBasic}}=== |
|||
<lang PureBasic>Procedure isPrime(v.i) |
|||
If v <= 1 : ProcedureReturn #False |
|||
ElseIf v < 4 : ProcedureReturn #True |
|||
ElseIf v % 2 = 0 : ProcedureReturn #False |
|||
ElseIf v < 9 : ProcedureReturn #True |
|||
ElseIf v % 3 = 0 : ProcedureReturn #False |
|||
Else |
|||
Protected r = Round(Sqr(v), #PB_Round_Down) |
|||
Protected f = 5 |
|||
While f <= r |
|||
If v % f = 0 Or v % (f + 2) = 0 |
|||
ProcedureReturn #False |
|||
EndIf |
|||
f + 6 |
|||
Wend |
|||
EndIf |
|||
ProcedureReturn #True |
|||
EndProcedure |
|||
Procedure fib(nr.i) |
|||
If nr = 0 : ProcedureReturn 0 |
|||
ElseIf nr = 1 : ProcedureReturn 1 |
|||
ElseIf nr > 1 : ProcedureReturn fib(nr-1) + fib(nr-2) |
|||
EndIf |
|||
EndProcedure |
|||
If OpenConsole() |
|||
Define i.i = 0, cont.i = 0 |
|||
PrintN("The first 9 Prime Fibonacci numbers: ") |
|||
Repeat |
|||
i + 1 |
|||
num = fib(i) |
|||
If isprime(num) |
|||
cont + 1 |
|||
If cont < 10 |
|||
Print(Str(num) + " ") |
|||
Else |
|||
Break |
|||
EndIf |
|||
EndIf |
|||
ForEver |
|||
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
|||
CloseConsole() |
|||
EndIf</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
==={{header|Yabasic}}=== |
|||
<lang yabasic>sub isPrime(v) |
|||
if v < 2 then return False : fi |
|||
if mod(v, 2) = 0 then return v = 2 : fi |
|||
if mod(v, 3) = 0 then return v = 3 : fi |
|||
d = 5 |
|||
while d * d <= v |
|||
if mod(v, d) = 0 then return False else d = d + 2 : fi |
|||
wend |
|||
return True |
|||
end sub |
|||
sub fib(nr) |
|||
if nr = 0 then return 0 : fi |
|||
if nr = 1 then return 1 : fi |
|||
if nr > 1 then return fib(nr-1) + fib(nr-2) : fi |
|||
end sub |
|||
i = 0 |
|||
cont = 0 |
|||
print "The first 9 Prime Fibonacci numbers: " |
|||
do |
|||
i = i + 1 |
|||
num = fib(i) |
|||
if isPrime(num) then |
|||
cont = cont + 1 |
|||
if cont < 10 then |
|||
print num, " "; |
|||
else |
|||
break |
|||
end if |
|||
end if |
|||
loop |
|||
end</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |