• Welcome to Valhalla Legends Archive.
 

[VB6] IsPrime() and SquareRoot() functions!

Started by Joe[x86], June 23, 2005, 11:05 PM

Previous topic - Next topic

Joe[x86]

I wrote these myself, so if you want to give credit, it goes to me, but hey, I got bored. I don't really care if I get credit or not, because its not worth it. =)

I made these well commented with good coding style and tabbing for your mutilation enjoyment.

Private Function IsPrime(L As Long) As Boolean
    Dim I As Integer, Root As Integer   '// Declare variables, for use with Option Explicit
    Root = SquareRoot(L)                '// If we don't do this, VB will re-figure the square root for each I completed. Ugh.
    For I = 2 To Root                   '// When we hit the square root, we start doing numbers over again. Thats just not fun. We start at
                                        '// two, because all numbers are divisible by themselves, and if we used one, all numbers would be composite
        If Int(L / I) = (L / I) Then    '// If L divided by I rounded down to the nearest whole is the same as L divided by I.. (I is our place in the loop)
            Let IsPrime = False         '// Then we have found a number its divisible by and it isn't prime
            Exit Function               '// If its not prime, it won't change, so just give up. =)
        End If
    Next I
    Let IsPrime = True                  '// If its still prime, its prime.
End Function

Private Function SquareRoot(L As Long) As Long
    Let SquareRoot = L ^ 0.5            '// 1 x 2 = 2 (squaring), so the opposite is 1 / 2, so raise it to 0.5. You've just gotta love math.
End Function



EDIT -
If anyone wants it, for some odd reason, heres a wrapper for the function to check the numbers between 1 and 10,000.
Option Explicit

Private Sub Form_Load()
    Dim L As Long
    Call ClearFile("C:\Prime.txt")
    For L = 1 To 10000
        If IsPrime(L) Then Call AppendFile("C:\Prime.txt", CStr(L))
    Next L
    End
End Sub

Private Sub ClearFile(File As String)
    Open File For Output As #1: Close #1
End Sub

Private Sub AppendFile(File As String, Data As String)
    Open File For Append As #1
        Print #1, Data
    Close #1
End Sub
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

You should make it so that you don't use SquareRoot (the function) at every loop iteration inside of IsPrime.  Like:

Dim root = SquareRoot(L)
For I = 2 To root
..
Next


You should also break out of the loop after you set Prime = false.

I don't trust VB to optimize out the repeated call to SquareRoot.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

For I = 2 To SquareRoot(L)

I already do that.

I considered breaking out of the loop, but eh. Yeah, I'll do that.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Joe[x86]

Updated code with MyndFyre's suggestions.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

OnlyMeat

#4
Quote from: Vote Joe! on June 23, 2005, 11:05 PM

        If Int(L / I) = (L / I) Then    '// If L divided by I rounded


You could possibly optimize that line by using the mod operator, which would mean you could eliminate a division.


if ( (L mod I) = 0 ) then // If there is no remainder then L can be equally divided by I (provided I is never equal to L that is), and therefore means it's not prime.


Someone will have to confirm this ;)

Tontow


TehUser

Not that it's especially important, but VB has a built-in square root function (Sqr).

dxoigmn

#7
Quote from: OnlyMeat on June 23, 2005, 11:53 PM
You could possibly optimize that line by using the mod operator, which would mean you could eliminate 2 divisions.

Generally, if you're trying to find a remainder that means you're going to have to do division.

This implementation is a really naive way of doing a primality test and asymptotically slow.  Faster primality tests are probablistic in nature (i.e. not deterministic). But a simple optimization is to skip every even number (except 2 of course).

OnlyMeat

Quote from: dxoigmn on June 24, 2005, 05:30 PM
Generally, if you're trying to find a remainder that means you're going to have to do division.

That is true, i should have read my statement back again.
It should have said, it eliminates a division.

You have any thoughts of your own on this subject, or are you only capable of regurgitating other peoples opinions/lexical definitions?

Funny enough the first page on a google search of 'primality test' came up with this page :-
http://mathworld.wolfram.com/PrimalityTest.html

Now normally i wouldn't do this, but i was impressed enough with your statement to actually look up primality test. From the first two paragraphs it's quite obvious that you are simply regurgitating opinions/information from that or another web page.

Examples:-

Example 1.
Quote from: dxoigmn on June 24, 2005, 05:30 PM
This implementation is a really naive way of doing a primality test and asymptotically slow.

Quote from: http://mathworld.wolfram.com/PrimalityTest.html
(2002) unexpectedly discovered a polynomial time algorithm for primality testing that has asymptotic complexity.

Example 2.
Quote from: dxoigmn on June 24, 2005, 05:30 PM
Faster primality tests are probablistic in nature (i.e. not deterministic)

Quote from: dxoigmn on June 24, 2005, 05:30 PM
Probabilistic tests can potentially (although with very small probability) falsely identify a composite number as prime (although not vice versa). However, they are in general much faster than deterministic tests.

I wouldn't have said anything, but you copied the terminology used as well. Next time either find a more obscure article or actually form your own opinions on the subject.


dxoigmn

#9
Quote from: OnlyMeat on June 24, 2005, 06:54 PM
You have any thoughts of your own on this subject, or are you only capable of regurgitating other peoples opinions/lexical definitions?

Funny enough the first page on a google search of 'primality test' came up with this page :-
http://mathworld.wolfram.com/PrimalityTest.html

Now normally i wouldn't do this, but i was impressed enough with your statement to actually look up primality test. From the first two paragraphs it's quite obvious that you are simply regurgitating opinions/information from that or another web page.

I wouldn't have said anything, but you copied the terminology used as well. Next time either find a more obscure article or actually form your own opinions on the subject.

When you're formally educated at an Ivy League college by a professor who is prominent within the number theory field and mentioned numerous times on the mathworld webpage and has created his own (with others) primality test, you start to pick up the terminology.  Additionally, I wasn't stating opinion, I was stating fact. Have anything else to say?

MyndFyre

Quote from: dxoigmn on June 24, 2005, 07:00 PM
When you're formally educated at an Ivy League college by a professor who is prominent within the number theory field and mentioned numerous times on the mathworld webpage and has created his own (with others) primality test, you start to pick up the terminology.  Additionally, I wasn't stating opinion, I was stating fact. Have anything else to say?

That's funny.  Now I'm not the only one who's been accused of trying to use terminology that is not my own when talking about something I've been formally educated in!  :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

OnlyMeat

#11
Quote from: dxoigmn on June 24, 2005, 07:00 PM
When you're formally educated at an Ivy League college by a professor who is prominent within the number theory field and mentioned numerous times on the mathworld webpage and has created his own (with others) primality test, you start to pick up the terminology.  Additionally, I wasn't stating opinion, I was stating fact. Have anything else to say?

So what you are saying is, you are taught by a very renown professor in number theory math who did alot of work related to prime number algorithms. You then go around posting quotes from his work on forums, and palming it off like you wrote it independently. I wouldn't take pride in regurgitating someone elses work, you could train a parrot to do that.

Thats really original and intelligent. If thats the kind of thing you learn at an Ivy League college (I haven't even got a clue what one of those is btw, some high price american school?). If it is i think you could save yourself some money and just go to the library instead.

Adron

Quote from: OnlyMeat on June 25, 2005, 12:36 AM
So what you are saying is, you are taught by a very renown professor in number theory math who did alot of work related to prime number algorithms. You then go around posting quotes from his work on forums, and palming it off like you wrote it independently. I wouldn't take pride in regurgitating someone elses work, you could train a parrot to do that.

What he's saying is that to anyone more knowledgeable in the subject than you are, this is part of basic education. Probabilistic prime number testing is well known and you can read about it in any number of places. What should he do? Call it "random indivisible integer checking" just to pretend it's something noone else knows about?

I better not tell you that 1 + 1 = 2 or you'll scream about how I'm regurgitating the top secret work of grade 1 math book authors....

You are very close to being banned from this forum btw :D

l)ragon

Quote from: OnlyMeat on June 25, 2005, 12:36 AM
Quote from: dxoigmn on June 24, 2005, 07:00 PM
When you're formally educated at an Ivy League college by a professor who is prominent within the number theory field and mentioned numerous times on the mathworld webpage and has created his own (with others) primality test, you start to pick up the terminology.  Additionally, I wasn't stating opinion, I was stating fact. Have anything else to say?

So what you are saying is, you are taught by a very renown professor in number theory math who did alot of work related to prime number algorithms. You then go around posting quotes from his work on forums, and palming it off like you wrote it independently. I wouldn't take pride in regurgitating someone elses work, you could train a parrot to do that.

Thats really original and intelligent. If thats the kind of thing you learn at an Ivy League college (I haven't even got a clue what one of those is btw, some high price american school?). If it is i think you could save yourself some money and just go to the library instead.

I donno what is up with you lately but people are here for solutions and such, not to be called liers and tryed to be proven wrong when infact they are right. I come here to read some intelligent conversation not mindless nonsence and theres been plenty of mindlessness going on here lately. 90% of the time if your willing to learn something, which is what the teacher is ther for (to teach), what you are taught over a period of time is bound to rub off on you.

Your negativity is starting to rub off on a few people around here now as it is, so can you give up on the repetitive (I'm right your wrong/you have regurgitated shit bellowing through your mouth/not reading the post or quote befor seeing the whole picture, before you post) posts, I don't know if it is just your time of the month or what OnlyMeat but this is just getting to be a bit retarded, are you like an imposter of the old OnlyMeat or what?..

* l)ragon waits for his post to get removed ;p
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

UserLoser.


Private Sub ClearFile(File As String)
    Open File For Output As #1: Close #1
End Sub

Private Sub AppendFile(File As String, Data As String)
    Open File For Append As #1
        Print #1, Data
    Close #1
End Sub


Bad bad bad.  Grab a value from FreeFile, store it in some Integer and use it as the file number...!