'VBScript program to display the prime numbers PrimeNumberLimit = inputbox("Enter the range till (ex: 50):") 'Getting the limit as an input from the user PrimeNumberList="Prime number list is:" 'Initialising the list of prime numbers For Ctr1=1 to PrimeNumberLimit PrimeFlag=True ' Initialising a Flag variable If Ctr1>=4 Then For Ctr2=2 to Ctr1/2 If Ctr1 mod Ctr2 = 0 Then 'Checking the reminder PrimeFlag=False 'Not a prime number End If Next End If If PrimeFlag=True Then PrimeNumberList=PrimeNumberList&" "&Ctr1 'Collecting the prime numbers End If Next Msgbox PrimeNumberList 'Displaying the result

**Program Explained**

Line 2: Getting the input from the user and storing it in a variable.

Sample output:

Line 5: Initializing the list of prime numbers in a string.

Line 8: Checking for all the numbers starting from 1 till user's input

Line 10: Initializing a flag variable.

Line 13: Excluding 1,2,3 the first three prime numbers.

Lines 15-22: Checking for the reminder, and if it is zero we conclude it is not a prime number.

Lines 28-33: Collecting all the prime numbers in a string.

Line 37: Displaying the list of prime numbers.

Result:

More such VB Scripts here.

How about this:

ReplyDelete'Getting the limit as an input from the user

PrimeNumberLimit = Inputbox("Enter the range to check Prime no : ")

'Initialising the list of prime numbers

PrimeNumberList="Prime number list is:"

For Ctr1=1 to PrimeNumberLimit step 2 'check only odd nos

PrimeFlag=True ' Initialising a Flag variable

If Ctr1>=4 Then

For Ctr2=2 to Ctr1/2

If Ctr1 mod Ctr2 = 0 Then 'Checking the reminder

PrimeFlag=False 'Not a prime number

Exit For 'no need to check further

End If

Next

End If

If PrimeFlag=True Then

PrimeNumberList=PrimeNumberList&" "&Ctr1

End If

Next

Msgbox PrimeNumberList

'Displaying the result

email: rajinder500@gmail.com

is 2 a primenumber or not?

Delete@ Anonymous : yes it is, in fact 2 is the only even prime number.

DeleteHi , above code shows 1 as Prime number But Wiki and other Google search page indicates 1 is Not a Prime Number ...

ReplyDeleteWiki : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Se we have to start count of "Ctr1" variable from 2 i think so ....

for j=1 to 50 step 1

ReplyDeletec=0

for i=1 to j step 1

if j mod i= 0 then 'divisibility ruke for prime remeinder is 0 only for 2 values'

c=c+1

end if

next

if c= 2 then

temp=temp&j&vbnewline

end if

next

msgbox temp

for j=1 to 50 step 1

ReplyDeletec=0

for i=1 to j step 1

if j mod i= 0 then 'using divisiblity rule of prime no. which have deir remainder 0 only for 2 values d no. n itself

c=c+1

end if

next

if c= 2 then

temp=temp&j&vbnewline

end if

next

msgbox temp

for j=1 to 50 step 1

ReplyDeletec=0

for i=1 to j step 1

if j mod i= 0 then

c=c+1

end if

next

if c= 2 then

temp=temp&j&vbnewline

end if

next

msgbox temp

' Performance improvements to generate primes significantly faster than the previous script

ReplyDelete'VBScript program to display the prime numbers

PrimeNumberLimit = inputbox("Enter the range till (ex: 50):")

'Getting the limit as an input from the user

wscript.echo Now()

' echo the start time of the script

PrimeNumberList="Prime number list is:"

'Initialising the list of prime numbers

PrimeNumberList = "2,3,5"

' Initialise list with numbers we now to be prime but do not want the loop testing.

For isPrime=6 to PrimeNumberLimit

PrimeFlag=True

' Initialising the Prime Flag variable

If Right(isPrime,1) = "1" or Right(isPrime,1) = "3" or Right(isPrime,1) = "7" or Right(isPrime,1) = "9" Then

' We know that prime numbers beyond 5 have a last digit which is either 1, 3, 7, or 9

' as all numbers greater than 5 whose last digit is 0, 2, 4, 6, 8 are divisible by 2 and all

' numbers greater than 5 whose last digit is 5 is divisible by 5.

For primeDivisor=2 to round(sqr(isPrime))+1

' We don't need to use all the numbers to 1/2 of the number to be tested - we only need

' test to the whole number nearest its square root.

If isPrime mod primeDivisor = 0 Then 'Checking the reminder

PrimeFlag=False

'Not a prime number

Exit For

End If

Next

Else

PrimeFlag=False

' If the number's last digit is 0, 2, 4, 5, 6, or 8 then it is not prime.

End If

If PrimeFlag=True Then

i = i + 1

'wscript.echo isPrime

PrimeNumberList=PrimeNumberList & "," & isPrime

'Collecting the prime numbers

if i mod 20 = 0 then

wscript.echo PrimeNumberList

PrimeNumberList = ""

End If

' collect and print the prime numbers in blocks of 20 separated by commas.

End If

Next

if not i mod 20 = 0 then wscript.echo PrimeNumberList

' if the script didn't print out the last group of primes, print it now that the main loop has ended.

wscript.echo Now()

' echo the end time of the script

[/code]