Chowla numbers: Difference between revisions

→‎{{header|Visual Basic .NET}}: extended range of perfect numbers using arbitrary precision library
m (→‎{{header|Perl 6}}: Remove odd editing debris)
(→‎{{header|Visual Basic .NET}}: extended range of perfect numbers using arbitrary precision library)
Line 959:
33,550,336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000
</pre>
===More Cowbell===
{{libheader|System.Numerics}}One can get a little further, but that 8th perfect number takes nearly a minute to verify. The 9th takes longer than I have patience. If you care to see the 9th and 10th perfect numbers, change the 31 to 61 or 89 where indicated by the comment.
<lang vbnet>Imports System.Numerics
 
Module Program
Function chowla(n As Integer) As Integer
chowla = 0 : Dim j As Integer, i As Integer = 2
While i * i <= n
If n Mod i = 0 Then j = n / i : chowla += i : If i <> j Then chowla += j
i += 1
End While
End Function
 
Function chowla1(ByRef n As BigInteger, x As Integer) As BigInteger
chowla1 = 1 : Dim j As BigInteger, lim As BigInteger = BigInteger.Pow(2, x - 1)
For i As BigInteger = 2 To lim
If n Mod i = 0 Then j = n / i : chowla1 += i : If i <> j Then chowla1 += j
Next
End Function
 
Function sieve(ByVal limit As Integer) As Boolean()
Dim c As Boolean() = New Boolean(limit - 1) {}, i As Integer = 3
While i * 3 < limit
If Not c(i) AndAlso (chowla(i) = 0) Then
Dim j As Integer = 3 * i
While j < limit : c(j) = True : j += 2 * i : End While
End If : i += 2
End While
Return c
End Function
 
Sub Main(args As String())
For i As Integer = 1 To 37
Console.WriteLine("chowla({0}) = {1}", i, chowla(i))
Next
Dim count As Integer = 1, limit As Integer = CInt((10000000.0)), power As Integer = 100,
c As Boolean() = sieve(limit)
For i As Integer = 3 To limit - 1 Step 2
If Not c(i) Then count += 1
If i = power - 1 Then
Console.WriteLine("Count of primes up to {0,10:n0} = {1:n0}", power, count)
power = power * 10
End If
Next
count = 0
Dim p As BigInteger, k As BigInteger = 2, kk As BigInteger = 3, st As DateTime = DateTime.Now
For i As Integer = 2 To 31 ' if you dare, change the 31 to 61 or 89
If {2, 3, 5, 7, 13, 17, 19, 31, 61, 89}.Contains(i) Then
p = k * kk
If chowla1(p, i) = p Then
Console.WriteLine("{0,25:n0} is a number that is perfect", p)
st = DateTime.Now
count += 1
End If
End If
k = kk + 1 : kk += k
Next
Console.WriteLine("There are {0} perfect numbers <= {1:n0}", count, 25 * BigInteger.Pow(10, 18))
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</lang>
{{out}}
<pre>chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
Count of primes up to 100 = 25
Count of primes up to 1,000 = 168
Count of primes up to 10,000 = 1,229
Count of primes up to 100,000 = 9,592
Count of primes up to 1,000,000 = 78,498
Count of primes up to 10,000,000 = 664,579
6 is a number that is perfect
28 is a number that is perfect
496 is a number that is perfect
8,128 is a number that is perfect
33,550,336 is a number that is perfect
8,589,869,056 is a number that is perfect
137,438,691,328 is a number that is perfect
2,305,843,008,139,952,128 is a number that is perfect
</pre>