TheBigDog Posted March 4, 2006 Report Posted March 4, 2006 I am starting this thread to share snippets of code that I have written with those who may be interested, and to help/get help with writing in vb.net. I am not a professional programmer, but I play one at work. I am a hobbiest. To me getting a desired response from a computer is like solving a puzzle. And I dig puzzles. The first snippet here is a function for finding if a number is true or false. Function IsPrime(ByVal Check As Long) As Boolean 'this is a function that checks if a number is prime 'first we check the obvious, 'if the number is less than two or a non integer, then it is not prime If Check < 2 Or Int(Check) <> Check Then Return False 'then we cheat and set the first prime number to 2 KnownPrimes(1) = 2 If Check = 2 Then Return True 'then we begin checking against our known list of primes Dim a as Long = 0 Do a += 1 'if the number is evenly divisible by a known prime then it is not prime If Check / KnownPrimes(a) = Int(Check / KnownPrimes(a)) Then Return False 'if we get past the sqrt of the check number 'and it still isn't evenly divisible 'then the number is prime If Check < KnownPrimes(a) ^ 2 Then Return True 'when we have exhausted our prime numbers, 'but the target is not reached yet 'then we need to find new primes, add them to the list, and keep searching Loop Until a = KnownPrimes.Length - 1 'begin expanding the list of prime numbers Dim Target as Integer = KnownPrimes(a) Do Target += 1 a = 0 Do a += 1 'if the number is evenly divisible by a known prime then it is not prime 'so we exit this loop and continue the bigger loop If Target / KnownPrimes(a) = Int(Target / KnownPrimes(a)) Then Exit Do 'if we get past the sqrt of the check number 'and it still isn't evely divisible 'then the number is prime If Target < KnownPrimes(a) ^ 2 Then ReDim Preserve KnownPrimes(KnownPrimes.Length) KnownPrimes(KnownPrimes.Length - 1) = Target If Check / Target = Int(Check / Target) Then Return False Exit Do End If Loop Until a = KnownPrimes.Length - 1 Loop Until Check < Target ^ 2 Return True End Function Inserting that into a program and then calling the function IsPrime(n) will return a true/false for the primeness of n. Its limitation is the values attainable by data type 'Long'. Bill Quote
alexander Posted March 6, 2006 Report Posted March 6, 2006 In c++ i usually use the seive to test for primity, something like: #include <iostream> #include <bitset> //namespace is too large to include all of it, wasting resources already with bitset using std::cin; using std::cout; int main() { const unsigned int MAX_VAL = 10000001; //set max val unsigned int max=0; //value to display primes up to cout << "Max value to check for primity up to (2-10000001): "; cin >> max; if(max >= MAX_VAL) { cout << "nValue entered too high, exiting...n"; return 0; } bitset<MAX_VAL+1> seive; //create a seive seive.set(); //set seive cout << "nDisplaying primes in range of 2 to " << max << ":n"; for(int i = 2; i<=max; i++) { if(seive.test(i)) //this actually tests for primity of a number { cout << i << " "; //display prime } } return 0; } simple yet effective... Quote
TheBigDog Posted March 6, 2006 Author Report Posted March 6, 2006 So seive.test() is a c++ native funtion to tell if a value is Prime. That is easier that building it yourself! One of the things I forgot to mention is that I did not declare KnownPrimes() in the function. I did that in the header of the program so that I could use the established list of primes within other functions. There is probably a better way of doing that, but is worked for me. Bill Quote
alexander Posted March 6, 2006 Report Posted March 6, 2006 no, no TBG, seive.test is a bitstring function, which is a library of C++, but not very native one, its a datascructure method, as long as you set it up right, it will do its job... Quote
TheBigDog Posted March 6, 2006 Author Report Posted March 6, 2006 no, no TBG, seive.test is a bitstring function, which is a library of C++, but not very native one, its a datascructure method, as long as you set it up right, it will do its job...Thus the separation between professional and hobbiest. :steering: I learn something new every day. Thanks! (If I had managed to get C++s in school I might have known this already:wink: ) Bill Quote
alexander Posted March 6, 2006 Report Posted March 6, 2006 naah, they dont usually touch advanced datastructures in beginning courses, you might get to classes in class, maybe even build a linked list or a queue or something, but probably wont even touch bitsets... Quote
Buffy Posted March 6, 2006 Report Posted March 6, 2006 Nonetheless, if you're going to have fun with programming, drop vb and go with C++ like alexander is encouraging you. Bitstrings are incredibly useful: #define MALE 1 #define MARRIED 2 #define HASKIDS 4 #define ISRICH 8 #define CONVICTIONS 16 #define HASEVILMOM 32 int BuffysIdealGuy = MALE | ISRICH; if (randomdude & HASEVILMOM > 0) { avoidlikeplague(); }Great for efficient, obscure code! Basic will grow hair on the palms of your hands and cause brain damage. You may use Java if you wish. Python has its advocates, and if you wanna do AI you gotta do Lisp. You have been warned. Opinions voiced, I'll shut up. Not Dim, but //,Buffy Quote
Jay-qu Posted March 6, 2006 Report Posted March 6, 2006 I took IT as a subject this year and will be doing solely java, what are your thoughts and opinions on this language compared with vb? Quote
Buffy Posted March 6, 2006 Report Posted March 6, 2006 ...and will be doing solely java, what are your thoughts and opinions on this language compared with vb?You should use VB if you would like to type more when you're writing code, have a harder time seeing nesting of code blocks, and want to use an extremely weak and limited implementation of objects... alexander would point out that you should use VB if you want to be locked in to a proprietary language controlled exclusively by the Evil Empire. :hyper: You have been warned! Again! Do not use VB, it causes rickets and leads to kidney stones. Goto considered harmful, :steering:Buffy Quote
Jay-qu Posted March 6, 2006 Report Posted March 6, 2006 thanks :steering: well thats all well and good, because im using java - but what about some advantages of it, not just disadvantages of vb.. Quote
Buffy Posted March 6, 2006 Report Posted March 6, 2006 ...but what about some advantages of it, not just disadvantages of vb...Just take my last post and put a ! in front of it silly! !(Facetious),Buffy Quote
Jay-qu Posted March 6, 2006 Report Posted March 6, 2006 lol ok i see what you mean..:steering: that was a pretty silly question Quote
alexander Posted March 6, 2006 Report Posted March 6, 2006 java has many problems of its own too, mainly that it is a black box that you put stuff into, and other stuff comes out... plus i hate its encouraging of bad programming practices, its total OO, its just not needed, memory hogging base that has to run to execute the code and so forth, i think i have posted my feelings on it many times before, i'll search if you want for more references... Quote
Jay-qu Posted March 6, 2006 Report Posted March 6, 2006 nevermind, thats good thanks :steering: - so just one more off topic question, what is your fav programming language? (maybe a new thread?) Quote
Qfwfq Posted March 6, 2006 Report Posted March 6, 2006 and want to use an extremely weak and limited implementation of objects...True about VB6 but false about VB.NET. If you were a real, true Dijkstraist you would have to prefer Java to C++ even. There's an old saying that a real C hacker doesn't disdain the odd goto when it makes things easier. :evil: BTW, what's wrong with a poor old chap, just 'cause he's unfortunate enough to have an evil mom? Do we have to start a new antidiscrimination movement here?:steering: :hyper: :gift: :wink: :) :( ;) :Alien: :Crunk: :hyper: :eight: :dog: :hihi: :hihi: :hihi: :hihi: Quote
TheBigDog Posted March 6, 2006 Author Report Posted March 6, 2006 Whoa! So I program using vb.net? I figured it would be nice to share peices of reusable code with others who may also be using vb.net. If you want to share similar code for other languages, feel free. But do we need to bash the language choice quite so hard? I would like some interaction without people feeling shy about joining in. Bill Quote
Buffy Posted March 6, 2006 Report Posted March 6, 2006 If you were a real, true Dijkstraist you would have to prefer Java to C++ even.True: Edsgar would give up multiple inheritance to avoid those gotos! :steering:BTW, what's wrong with a poor old chap, just 'cause he's unfortunate enough to have an evil mom? I'll date them maybe, but not marry them! :gift:I would like some interaction without people feeling shy about joining in.Sorry about that! With snotty opinionated folks like me and alexander around here, that can be hard sometimes. VB.Net is as Q points out, much improved over VB6/ASPVscript, and has its disadvantages (belabored above), but if it works, go for it! That's the hacker code! And now back to our previously scheduled thread. (C'mon! Don't let us curmugeons stop you! Post! Really! You'll enjoy it!) Stifled, :hyper:Buffy Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.