GetPrimeNumbers – Evaluate the first N prime numbers

' Returns an array with the first N prime numbers'' Note: you can easily convert this routine to VB4 and VB5 by' returning the result array through an argument instead of' the return valueFunction GetPrimeNumbers(numberOfPrimes As Long) As Long()    Dim found As Long    Dim n As Long    Dim i As Long        If numberOfPrimes <= 0 Then Err.Raise 1002, , "Invalid argument"        ' we know the size of the result in advance    ReDim result(1 To numberOfPrimes) As Long        ' "2" is the first prime number    result(1) = 2: found = 1        n = 1    Do        ' all other prime numbers are odd, so we can skip even numbers        n = n + 2        ' let's check if N is a prime number        For i = 1 To found            If (n Mod result(i)) = 0 Then Exit For        Next        If i > found Then            ' no prime number < N is a divisor for N            ' therefore N is prime            found = found + 1            result(found) = n            If found = numberOfPrimes Then Exit Do        End If    Loop        GetPrimeNumbers = resultEnd Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may