Magnanimous numbers: Difference between revisions

Content deleted Content added
Added C# version
Added Visual Basic .NET version
Line 1,309: Line 1,309:
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
</pre>
</pre>

=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Imports System, System.Console

Module Module1

Dim np As Boolean()

Sub ms(ByVal lmt As Long)
np = New Boolean(CInt(lmt)) {} : np(0) = True : np(1) = True
Dim n As Integer = 2, j As Integer = 1 : While n < lmt
If Not np(n) Then
Dim k As Long = CLng(n) * n
While k < lmt : np(CInt(k)) = True : k += n : End While
End If : n += j : j = 2 : End While
End Sub

Function is_Mag(ByVal n As Integer) As Boolean
Dim res, rm As Integer, p As Integer = 10
While n >= p
res = Math.DivRem(n, p, rm)
If np(res + rm) Then Return False
p = p * 10 : End While : Return True
End Function

Sub Main(ByVal args As String())
ms(100_009) : Dim mn As String = " magnanimous numbers:"
WriteLine("First 45{0}", mn) : Dim l As Integer = 0, c As Integer = 0
While c < 400 : If is_Mag(l) Then
c += 1 : If c <= 45 OrElse (c > 240 AndAlso c <= 250) OrElse c > 390 Then Write(If(c <= 45, "{0,4} ", "{0,8:n0} "), l)
If c < 45 AndAlso c Mod 15 = 0 Then WriteLine()
If c = 240 Then WriteLine(vbLf & vbLf & "241st through 250th{0}", mn)
If c = 390 Then WriteLine(vbLf & vbLf & "391st through 400th{0}", mn)
End If : l += 1 : End While
End Sub
End Module</lang>
{{out}}
<pre>First 45 magnanimous numbers:
0 1 2 3 4 5 6 7 8 9 11 12 14 16 20
21 23 25 29 30 32 34 38 41 43 47 49 50 52 56
58 61 65 67 70 74 76 83 85 89 92 94 98 101 110

241st through 250th magnanimous numbers:
17,992 19,972 20,209 20,261 20,861 22,061 22,201 22,801 22,885 24,407

391st through 400th magnanimous numbers:
486,685 488,489 515,116 533,176 551,558 559,952 595,592 595,598 600,881 602,081 </pre>


=={{header|Wren}}==
=={{header|Wren}}==