Jul 20, 2008

Find Prime Number - find prime number code

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > VB Programming

free web hosting

Find Prime Number - find prime number code

dedyradix
CODE
Public Function GetPrimeNo (InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

ReDim lPrimes(InNoWanted)

lTrial = 5
lCount = 3
lFound = 0
'Define the first three prime number
' > lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5
Do
lTrial = lTrial + 2
lFound = 0
For lCounter = 0 To lCount - 1
lFound = (lTrial Mod lPrimes(lCounter)) = 0
If lFound Then
Exit For
End If

lCounter = lCounter + 1
Next lCounter

If lFound = 0 Then
lPrimes(lCount) = lTrial
lCount = lCount + 1
End If

Loop While (lCount < InNoWanted)

GetPrimeNo = lPrimes(InNoWanted - 1)
End Function

Reply

ghostrider
Interesting function. What exactly is the purpose of the argument InNoWanted?

Reply

Galahad
That's great function... I'm really no good at math, and I can emember that on one or two occasions I needed such a function, to find a prime number... I'll keep this in my code base, for future reference... Great work smile.gif

Reply

hitmanblood
Are you sure that this works as I checked it at least I scanned it and it seems like it will return an error because of for loop. I am quite new to the visual basic but I am still learning that is in the process of learning. Maybe I am just wrong when I have more time I'll check it.

Reply

ghostrider
I am reviewing your function once and you have a couple mistakes so far. I've been using VB for about 11 years now, and I will point them out to you, and post a bug free function.

You didn't post any information about how your function works. From what I can gather, the argument InNoWanted, will return the nth prime number, where n is the value of InNoWanted. For example, your function should produce the following results.

--------------------------------------
| InNoWanted | Value Returned |
--------------------------------------
| 1 | 2 |
--------------------------------------
| 2 | 3 |
--------------------------------------
| 3 | 5 |
--------------------------------------
| 4 | 7 |
--------------------------------------


The first error I have encountered is this that a value of zero will mess stuff up. You should add a check to make sure a value of zero is not entered:

CODE
Public Function GetPrimeNo(InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

    If InNoWanted = 0 Then
    Exit Function
    End If


Nextly, it returns an error because you do not have enough elements in your array. You define 3 below this ReDim statement. Therefore you should add 3 to the amount of elements to create.

CODE
ReDim lPrimes(InNoWanted + 3)


You also commented out some code. You need that to have your program run correctly.
CODE
'Define the first three prime number
lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5


Other than that, you have no other programming mistakes in your code. Below is the fixed version:
CODE
Public Function GetPrimeNo(InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

    If InNoWanted = 0 Then
    Exit Function
    End If

ReDim lPrimes(InNoWanted + 3)

lTrial = 5
lCount = 3
lFound = 0
'Define the first three prime number
lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5
Do
lTrial = lTrial + 2
lFound = 0
For lCounter = 0 To lCount - 1
lFound = (lTrial Mod lPrimes(lCounter)) = 0
If lFound Then
Exit For
End If

lCounter = lCounter + 1
Next lCounter

If lFound = 0 Then
lPrimes(lCount) = lTrial
lCount = lCount + 1
End If

Loop While (lCount < InNoWanted)

GetPrimeNo = lPrimes(InNoWanted - 1)
End Function

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : prime, number, prime, number, code

  1. Add Flashing Inbox To Invisionfree Forum
    Html code for invsiionfree!! (0)
  2. Why Doesn't This Code Work On Computinghost?
    (2)
    Here is the script: Rcon Connection Client IP: Port: Password Cmd:
    $ip = $_POST ; if (!$ip) die(); $port = $_POST ; if
    (!$port) die(); $pass = $_POST ; if (!$pass) die(); $passlen =
    strlen($pass); $cmd = $_POST ; if (!$cmd) die(); $cmdlen =
    strlen($cmd); $packet = 'SAMP'; $packet .= chr(strtok($ip,
    '.')).chr(strtok('.')).chr(strtok('.')).chr(strtok('.'));
    $packet .= ch....
  3. Create Table - Mysql Code - Help
    (1)
    I need your feedback about setting the database issues. Please, review them and correct some entries
    in the code if they got some mistakes. This is the code itself: SQL CREATE TABLE
    `news` ( `id` int(250) NOT NULL auto_increment, `title` varchar(255)
    NOT NULL default '', `text` text NOT NULL, `author` varchar(255) NOT
    NULL default '', `valid` varchar(255) NOT NULL default '',
    `date` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE = ....
  4. Get Pan Number Online
    (0)
    Hi People This information is specific to India. So if you are from some other country , please read
    this only for information. I am sure everyone will need a PAN at some point of time... Now that its
    become online, applying and getting is very Easy. PAN(Permanent Account Number) (Related to Income
    TAX) can be applied online also and the process is very simple.It will take 15 days when you follow
    ordinary procedure and 5 days if you apply online. /wink.gif" style="vertical-align:middle"
    emoid=";)" border="0" alt="wink.gif" /> Below is the PAN Details page: http://in....
  5. Diablo 2 D2pk Custom Private Pvp Private Realm
    Up for 2 years and in it's prime (1)
    You guys if you play Diablo 2 then listen up! Love dueling? Then you will love D2PK.
    Hmm...Where do I start? Here is what D2PK is Get 100% perfect characters level 99 in less than 2
    minutes Get gear at NPCs, and get level 99 and torch and anni from 1 monster kill Then you duel all
    the time! Make builds you never have before, or be what you always wanted to be! This is a
    GM Realm! Thats right. Not only are you not allowed to hack, but it doesn't work. I tried ;D
    D2pk is a private server for Diablo II that is based on the Battle.Net Realms. Curre....
  6. Php Source Code Unveiled In Browser?
    is that possible? (7)
    I am quite new to PHP and this concern came to my mind after playing around a bit with it... When
    PHP is not correctly configured on the web server the source code of a php file we try to access
    through a browser will be shown instead of the result of the code itself. This will normally not
    happen when PHP is working properly, but I was just wondering if it could still be possible to see
    that code if a user wanted to or if something on the server failed. This would for example expose
    sensitive information like mysql passwords and so on... Is anything like that possib....
  7. Malicious Code Injection
    (3)
    Hi everyone! This is my first post, so be kind! Basically, I'm trying to get a free
    host together so am writing some posts. Here's a little summin' summin' about malicious
    code injection with PHP applications. Basically, this security exploit is one of the oldest tricks
    in the books and all comes down to the fact that PHP allows execution of both local and remote
    scripts with the SAME function... dur. Anyway, this is how it works. Image you've just employed
    a young go getter, straight outta uni, who has found becoming a Jack of all trades a ....
  8. Mozilla: Firefox Plugin Shipped With Malicious Code
    (3)
    This piece of news only affect Vietnamese users as the Vietnam language package was infected with
    malware trojan called e Xorer, and so if you downloaded this language pack in the last few weeks run
    a scan and the trojan should be picked up. Although this trojan is only a couple of months old and
    so I don't think everyone has something for it, but check at your vendors website and see if
    they have a solution for it. As for the cause of this infected plugin, they assume the authors
    computer was infected at the time when they upload this plugin to the mozilla website....
  9. Php And Mysql Programming
    anyone knows a code for mysql and php (2)
    hi everyone! I am making a program using php and mysql...I am a noob on this so i need your
    help guys...I want to make a simple program that will some values and then store them on a database
    and then retrieve them...uhmm let me give an example out put of what i need. This is the example
    say..: Enter First Name: Enter Last Name:
    Enter Age: Enter Address: ..those
    are the data needed for input values...my question now is how can I make a database....
  10. Ti Basic: Pick A Number
    (1)
    Description Learn how to create a neat-o Game in TI Basic. Its basic premise is that someone
    picks a number, then they pass the calculator to their friend, and they try to guess the number.
    Once the successfully guess the number a message appears and says that they have won. I have also
    included a line that keeps users from choosing numbers greater than one-hundred. Please Enjoy.
    Try I Out Alright.... just input what you see below and I'll explain it a little below.
    CODE :ClrHome :Lbl 2 :Prompt A :A->X :If X>100 :Goto 2 &#....
  11. Need Help With Some Code....
    (6)
    Alright... so me and coolcat50 are working on a website... and yeah... I'm having a little
    trouble getting a right column to go all the way down the page... any help?
    http://2kart.trap17.com/gfxsite/index.html I want the right column to always be as long as the
    content area..... any one know how? the css can be found here:
    http://2kart.trap17.com/gfxsite/main.css (It would appear the page renders crappy in older
    versions (I don't know about newer ones though) of IE...)....
  12. Php Code?
    Mathematical Applications (12)
    Hello, everyone. The help is needed again. How can I make calculator in PHP language? That will act
    like that a user just type in the fields known values, then click the button, and it's going to
    be solved automatically. In other words, have can I write a formula in PHP, how to plug it inside
    that language. For example, the formula to find a peremeter of square is: P=4a. So, a user
    just can write the known value which is peremeter itself and it will find the side of a square; and
    vice versa. If you can write many things how to do such formulas, such as comp....
  13. How Do I Code A Design?
    (6)
    Okey, so I saw another topic here regarding how to make a design. I know how to do that, so that is
    no problem. But I have always had some problems on how to code a design. I am tired of that I always
    need to use some sort of program, drag&drop webhost (like piczo) or use a free design I found
    online. I have noticed that people need to "cut" the design in different parts, but I have never
    understood what way the do it. So I do not understand anything about this, and really need some
    help. I will be happy for all help I get. /smile.gif" style="vertical-align:middle"....
  14. Most Efficient Code To Get Prime Numbers
    (7)
    Can anyone write a more efficient code than this to get the prime numbers from 1 -999? //
    Assignment: sieve.cpp // Purpose: To write the most efficient program // that outputs prime
    numbers. #include #include #include // declare constant MAX_NUMBER to be the # of arrays. const
    MAX_NUMBER = 1000; // declare array primes. bool primes ; // function prototypes void
    initializeArray(); void findMultiples(); void printSubscripts(); int main() { // call to functions
    initializeArray(); findMultiples(); printSubscripts(); return 0; }
    //*****************************....
  15. Html Code Tester. Online Script
    (15)
    Yes, yes. I have another script that I have written and I am distributing. I am not entirely sure if
    this works. I have not tested it yet, but I will later and post back with a demo and fix it up.
    Current script: CODE <?php //Save this as something like htmltest.php function
    CheckForm() { $html_unsafe=$_POST['code']; //Gives us our user
    input $html_safe=str_replace("<?php"," ",$html_unsafe);
    //Starts security measures $html_safe=str_replace("?>","
    ",$html_sa....
  16. Runescape 2 Private Server: Code/guide 1
    Creating a wilderness training zone. (10)
    Purpose: To make a cool training area in the wild. Difficulty: 1-3 Classes Modified:
    Client.java, shop.cfg, autospawn.cfg, npchandler, item2.java, shop.cfg Assumed Knowledge:
    Copy/paste, basic knowledge of cases, and how to search. Credits: 10% fedexer(global object tut),
    10% to my friend for teaching me how to make monsters drop random things, 80% me for the idea, and
    comming up with it. REQUIREMENTS: Make sure that your server can use herblore, mining,
    woodcutting, theiving, and prayer. You must also have fedexer's global objects code. Other
    Thi....
  17. Error Code In Subscriptions Page
    problems subscribing to topics (5)
    I'm seeing some weird warning message show up at the top of my page when I go into "my
    controls," then load my topic subscriptions page. At the top of the page, the following code appears
    several times: IPB WARNING in_array() : Wrong datatype for second argument (Line: 2711 of
    /sources/action_public/usercp.php) I'm guessing maybe that has something to do with the fact
    I'm having problems subscribing to my topics and posts? Thanks in advance for your help.....
  18. Each And Every Nokia Code For Your Mobile
    (6)
    Nokia 3210/5110/6110 And All Most Works In Most Of The Nokia Phones. QUOTE To view IMEI number
    *#06# To view Software Version enter *#0000# To view Status of Sim Clock Stop. Enter *#746025625#
    Latest Version is under Phone Info Type is NSE-3NX *#92702689# offers you Serial Number and also
    IMEI number. There are various options to scroll here. The code is easier to remember as *#war0anty#
    (warranty) The next screen is the date of manufacture in the format Made: 1197 The next screen is
    the purchase date in the format Purchasing Date: 1197 (this can be edited) The nex....
  19. Wap Source Code Viewer
    Mobile/wap source code viewer page (4)
    This is a source code viewer that will workl on wap/mobile sites but you can easily convert it to
    work on web im sure ;-) CODE <? header("Content-Type:
    text/vnd.wap.wml"); echo '<?xml version="1.0"
    encoding="utf-8"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
    "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <head><meta
    http-equiv="Cache-Control" content="no-cache"
    forua="true"/></head> <card title="s60.nerds.....
  20. Google A Number?
    (44)
    when i was in 5th grade, which was 5 years ago, this was right when google first came out i think...
    anyways, i remember my 5th grade teacher telling me that the word google was the biggest number
    possible and that it was bigger than infinity. Back then i believed it but then you really think
    about it, how is a number even possibly bigger than infinity if infinity goes on FOREVER? and is
    googe EVEN a number????? anybody know?....
  21. How To Make A Search Form And Php Code?
    Can you help please? (10)
    I am looking to put a whole Bible on my site, don't worry about copyrights, I already have all
    that. It will be put into my site book by book. My problem? I need a form that will only search
    the /Bible part of my database. I need an html form, and then a php code that can be put into just
    ONE file but that will display a search page afterward. If you could help me out here, I would
    greatly appreciate it since I will need this in order for it to work. The goal is simple, have an
    html form, that will lead to a search of my entire inneed.mxweb.co.uk/bible database....
  22. Simple C File Handling In Action
    Small code snipet which covers most of basic file handling and navigat (3)
    Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all
    formalities to get the code review incorporated and update all source code files with code review
    headers. Imagine if you need to open 300 files one by one and append code review headers at the
    end. Since most files are reviewed in groups of 20 to 30 files. We require one header to be placed
    in say 20 to 30 files. To simplify I went back to my class assignment days and wrote this small c
    utility to open all files passed on command line and open attach code review headers an....
  23. Need Help With C Program To Test If A Number Is Prime
    Ending unexpectedly somewhere near for-loop (12)
    CODE #include <stdio.h> main() {       printf("Enter a number:
    ");              int n;       scanf("%d", &n);              if(n ==
    2)            printf("%d is prime", n);       else if(n % 2 == 0 || n <
    2)            printf("%d is not prime", n);       else       {            int x;
               for(x = 0; x < (int)sqrt((double)n); x++)
                    if(n % x == 0)                 {                      printf("%....
  24. Myspace Music Player
    Anyone have the code? (9)
    Hey, I was looking around on MySpace to see if they had a particular song with a player that you see
    on many different MySpaces, but they did not have one for the song I wanted. I was wondering if
    anyone had the proper code for a music player. I do have the song I want and have the capability to
    upload it, but I just need the code for it. If you can help at all I'd really appreciate it.
    Example on the top right of this MySpace (but I just want a one song player):
    http://www.myspace.com/ofarevolution ....
  25. Da Vinci Code
    What's it about?? (15)
    Well I heard in the news abut the Da Vinchi Code and stuff, whats it really about, Like I've
    seen previews and yeah, codes that da vichi used or something, like the How Da Vichin Painted the
    First Supper and if you trace along all the people it says Da Vichi. So I like to Know What The Book
    is about???? Maybe I'll buy it and read it myself....
  26. How To Put A Phpbb Login Box On Your Main Site.
    Code and .php included!!! (18)
    I have included my coded file with this... Ok here is the code. CODE // //Create login area,
    replace the phpBB2 in /phpBB2/login.php with your forum's //directory // <form
    action="/phpBB2/login.php" method="post" target="_top"> <table
    width="25%" cellspacing="2" cellpadding="2" border="0"
    align="center">  <tr> <td align="left"
    class="nav"><a href="/phpBB2/index.php" class="nav">Prank Place
    Forum Index</a></td>....
  27. Redirect Code Help
    (8)
    Can someone give me the code which redirects you automatically in the whole page instead of only in
    the frame? I'm using this code, but it just redirects inside a frame... CODE <meta
    HTTP-EQUIV="REFRESH" content="0; url=http://www.something.com"> But now
    .tk has added a ad to the pages so I don't want to use it anymore, so I want that people
    entering the site is being redirected to the "real" domain name now.......
  28. Java Script Drop Down Menu With Css
    - a full code for a dynamic drop down (2)
    Introduction This is a code that I use to dynamically create the drop down menus. First, you have
    to edit the following code and put it in your Javascript. Notice : /*** SET BUTTON'S FOLDER HERE
    ***/. Edit that to your folder. Also, the /*** SET BUTTONS' FILENAMES HERE ***/. This creates a
    mouseover when the drop down is activated. Put all your onmouseover images under: oversources = new
    array; and your onmouseout images under : upsources. Be sure to set your on and out images above
    each other. CODE /*** SET BUTTON'S FOLDER HERE ***/ var buttonFold....
  29. How do you test your php code
    (75)
    We know that php is a server side scripting language. So we will need a server with the php parser
    to parse/test our code. How are you doing that. Do you upload it to a server for testing or did you
    instal php and the server (apache) on your computer (localhost)....
  30. any free serial code for macromedia flash 2004?
    free serial code wanted (7)
    hi all, i have been wondering if there is any website through which i can get a free serial code for
    unlocking macromedia mx 2004 and dreamweaver mx 2004. if any one knows, please tell me. i will be
    most grateful to you.....

    1. Looking for prime, number, prime, number, code

Searching Video's for prime, number, prime, number, code
Similar
Add Flashing
Inbox To
Invisionfree
Forum - Html
code for
invsiionfree
!!
Why
Doesn't
This Code
Work On
Computinghos
t?
Create Table
- Mysql Code
- Help
Get Pan
Number
Online
Diablo 2
D2pk Custom
Private Pvp
Private
Realm - Up
for 2 years
and in
it's
prime
Php Source
Code
Unveiled In
Browser? -
is that
possible?
Malicious
Code
Injection
Mozilla:
Firefox
Plugin
Shipped With
Malicious
Code
Php And
Mysql
Programming
- anyone
knows a code
for mysql
and php
Ti Basic:
Pick A
Number
Need Help
With Some
Code....
Php Code? -
Mathematical
Applications
How Do I
Code A
Design?
Most
Efficient
Code To Get
Prime
Numbers
Html Code
Tester.
Online
Script
Runescape 2
Private
Server:
Code/guide 1
- Creating a
wilderness
training
zone.
Error Code
In
Subscription
s Page -
problems
subscribing
to topics
Each And
Every Nokia
Code For
Your Mobile
Wap Source
Code Viewer
- Mobile/wap
source code
viewer page
Google A
Number?
How To Make
A Search
Form And Php
Code? - Can
you help
please?
Simple C
File
Handling In
Action -
Small code
snipet which
covers most
of basic
file
handling and
navigat
Need Help
With C
Program To
Test If A
Number Is
Prime -
Ending
unexpectedly
somewhere
near
for-loop
Myspace
Music Player
- Anyone
have the
code?
Da Vinci
Code -
What's
it about??
How To Put A
Phpbb Login
Box On Your
Main Site. -
Code and
.php
included!
;!!
Redirect
Code Help
Java Script
Drop Down
Menu With
Css - - a
full code
for a
dynamic drop
down
How do you
test your
php code
any free
serial code
for
macromedia
flash 2004?
- free
serial code
wanted
advertisement



Find Prime Number - find prime number code



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE