 |
|
03-04-2006
|
#1 (permalink)
|
|
Doing the Impossible
Location: Madison, OH (when not in fantasy land)
|
Not Ranked
:
+0 / -0
0 score
vb.net Programming
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.
Code:
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
----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill
TheBigDog's recommended reading: The Science of Success - Charles G. Koch
A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."
|
|
03-05-2006
|
#2 (permalink)
|
|
Dedicated Smart-ass
Location: Just before 0xAA55
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
In c++ i usually use the seive to test for primity, something like:
Code:
#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...
----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.
Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.
Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.

|
|
03-05-2006
|
#3 (permalink)
|
|
Doing the Impossible
Location: Madison, OH (when not in fantasy land)
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
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
----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill
TheBigDog's recommended reading: The Science of Success - Charles G. Koch
A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."
|
|
03-05-2006
|
#4 (permalink)
|
|
Dedicated Smart-ass
Location: Just before 0xAA55
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
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...
----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.
Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.
Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.

|
|
03-05-2006
|
#5 (permalink)
|
|
Doing the Impossible
Location: Madison, OH (when not in fantasy land)
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
Quote:
|
Originally Posted by alexander
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.  I learn something new every day. Thanks!
(If I had managed to get C++s in school I might have known this already  )
Bill
----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill
TheBigDog's recommended reading: The Science of Success - Charles G. Koch
A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."
|
|
03-05-2006
|
#6 (permalink)
|
|
Dedicated Smart-ass
Location: Just before 0xAA55
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
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...
----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.
Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.
Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.

|
|
03-05-2006
|
#7 (permalink)
|
|
Resident Slayer
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
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:
Code:
#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
----------------
"If you do not agree with anything I say, I'll not only retract it, but deny under oath that I ever said it!"
__________________________________________________ ______________-- Tom Lehrer
"No Robbie, not Europe!"
Forum Administrator
Hypography Science Forums - Science for Boys and Girls! Its not for nothing that we hang out here.
|
|
03-06-2006
|
#8 (permalink)
|
|
Ancora Imparo
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
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?
----------------
Jay-qu
::Hypography Moderator of..
Chemistry, Physics & Mathematics, Astronomy & Cosmology, Space and Technology & gadgets Forums
"I don't think much of a man who is not wiser today than he was yesterday."
-Abraham Lincoln
Physics Guides - Physics Resources and help
|
|
03-06-2006
|
#9 (permalink)
|
|
Resident Slayer
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
Quote:
|
Originally Posted by Jay-qu
...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.
You have been warned! Again! Do not use VB, it causes rickets and leads to kidney stones.
Goto considered harmful, 
Buffy
----------------
"If you do not agree with anything I say, I'll not only retract it, but deny under oath that I ever said it!"
__________________________________________________ ______________-- Tom Lehrer
"No Robbie, not Europe!"
Forum Administrator
Hypography Science Forums - Science for Boys and Girls! Its not for nothing that we hang out here.
|
|
03-06-2006
|
#10 (permalink)
|
|
Ancora Imparo
|
Not Ranked
:
+0 / -0
0 score
Re: vb.net Programming
thanks  well thats all well and good, because im using java - but what about some advantages of it, not just disadvantages of vb..
----------------
Jay-qu
::Hypography Moderator of..
Chemistry, Physics & Mathematics, Astronomy & Cosmology, Space and Technology & gadgets Forums
"I don't think much of a man who is not wiser today than he was yesterday."
-Abraham Lincoln
Physics Guides - Physics Resources and help
|
|
 |
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
» Advertisement |
|
|
|