Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to find the sum of all the primes below two million in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Friend Shared Function isPrime(ByVal n As Integer) As Boolean
        If n < 2 OrElse (n Mod 2 = 0 AndAlso n <> 2) Then
            Return False
        End If

        Dim count As Integer = CInt(Math.Floor(Math.Sqrt(n)))

        For i As Integer = 3 To count Step 2

            If n Mod i = 0 Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim num As Long = 2000000, sum As Long = 0

        For i As Integer = 2 To num - 1

            If isPrime(i) Then
                sum += i
            End If
        Next

        Console.WriteLine("sum = " & sum)
    End Sub
End Class




' run:
'
' sum = 142913828922
'
 

 



answered Oct 29, 2023 by avibootz

Related questions

1 answer 103 views
1 answer 127 views
1 answer 121 views
1 answer 91 views
1 answer 90 views
...