How to resolve the algorithm Chowla numbers step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Chowla numbers step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

Chowla numbers are also known as:

The chowla number of   n   is   (as defined by Chowla's function):

The sequence is named after   Sarvadaman D. S. Chowla,   (22 October 1907 ──► 10 December 1995), a London born Indian American mathematician specializing in number theory.

German mathematician Carl Friedrich Gauss (1777─1855) said:

Chowla numbers can also be expressed as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Chowla numbers step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Imports System

Module Program
    Function chowla(ByVal n As Integer) As Integer
        chowla = 0 : Dim j As Integer, i As Integer = 2
        While i * i <= n
            j = n / i : If n Mod i = 0 Then chowla += i + (If(i = j, 0, j))
            i += 1
        End While
    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 : limit = 35000000
        Dim p As Integer, k As Integer = 2, kk As Integer = 3
        While True
            p = k * kk : If p > limit Then Exit While
            If chowla(p) = p - 1 Then
                Console.WriteLine("{0,10:n0} is a number that is perfect", p)
                count += 1
            End If
            k = kk + 1 : kk += k
        End While
        Console.WriteLine("There are {0} perfect numbers <= 35,000,000", count)
        If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
    End Sub
End Module


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
        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


  

You may also check:How to resolve the algorithm Longest common subsequence step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Compound data type step by step in the jq programming language
You may also check:How to resolve the algorithm Sorting algorithms/Radix sort step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Formal power series step by step in the jq programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the PARI/GP programming language