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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,700 questions

57,013 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 195 views
1 answer 221 views
1 answer 226 views
1 answer 190 views
1 answer 191 views
...