Pernicious numbers: Difference between revisions

Pernicious numbers in various dialects BASIC (BASIC256, Gambas and Yabasic)
(add RPL)
(Pernicious numbers in various dialects BASIC (BASIC256, Gambas and Yabasic))
Line 560:
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">n = 1
cont = 0
print "The following are the first 25 pernicious numbers:"
print
 
do
if isPernicious(n) then
print rjust(string(n), 3);
cont += 1
end if
n += 1
until cont = 25
 
print : print
print "The pernicious numbers between 888,888,877 and 888,888,888 inclusive are:"
print
for n = 888888877 to 888888888
if isPernicious(n) then print rjust(string(n), 10);
next n
end
 
function SumBinaryDigits(number)
if number < 0 then number = -number # convert negative numbers to positive
sum = 0
while number > 0
sum += number mod 2
number /= 2
end while
return sum
end function
 
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 isPernicious(number)
popcont = SumBinaryDigits(number)
return isPrime(popcont)
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim n As Integer = 1, count As Integer = 0
 
Print "The following are the first 25 pernicious numbers:\n"
Do
If isPernicious(n) Then
Print Format$(n, "###");
count += 1
End If
n += 1
Loop Until count = 25
Print "\n\nThe pernicious numbers between 888,888,877 and 888,888,888 inclusive are:\n"
For n = 888888877 To 888888888
If isPernicious(n) Then Print Format$(n, "##########");
Next
Print
 
End
 
Public Sub isPrime(ValorEval As Long) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Long = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function
 
Public Function SumBinaryDigits(number As Integer) As Integer
If number < 0 Then number = -number ' convert negative numbers to positive
Dim sum As Integer = 0
While number > 0
sum += number Mod 2
number \= 2
Wend
Return sum
End Function
 
Public Function isPernicious(number As Integer) As Boolean
Dim popCount As Integer = SumBinaryDigits(number)
 
Return isPrime(popCount)
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">n = 1
cont = 0
print "The following are the first 25 pernicious numbers:\n"
repeat
if isPernicious(n) then
print n using ("###");
cont = cont + 1
fi
n = n + 1
until cont = 25
 
print "\n\nThe pernicious numbers between 888,888,877 and 888,888,888 inclusive are:\n"
for n = 888888877 to 888888888
if isPernicious(n) print n using("##########");
next n
print
end
sub SumBinaryDigits(number)
if number < 0 number = -number // convert negative numbers to positive
sum = 0
while number > 0
sum = sum + mod(number, 2)
number = int(number / 2)
wend
return sum
end sub
 
sub isPrime(v)
if v < 2 return False
if mod(v, 2) = 0 return v = 2
if mod(v, 3) = 0 return v = 3
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 isPernicious(number)
popcont = SumBinaryDigits(number)
return isPrime(popcont)
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Befunge}}==
2,122

edits