Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] IsPrime() and SquareRoot() functions!

AuthorMessageTime
JoeTheOdd
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.

[code]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[/code]


EDIT -
If anyone wants it, for some odd reason, heres a wrapper for the function to check the numbers between 1 and 10,000.
[code]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[/code]
June 24, 2005, 4:05 AM
Myndfyr
You should make it so that you don't use SquareRoot (the function) at every loop iteration inside of IsPrime.  Like:
[code]
Dim root = SquareRoot(L)
For I = 2 To root
..
Next
[/code]

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.
June 24, 2005, 4:18 AM
JoeTheOdd
For I = 2 To SquareRoot(L)

I already do that.

I considered breaking out of the loop, but eh. Yeah, I'll do that.
June 24, 2005, 4:21 AM
JoeTheOdd
Updated code with MyndFyre's suggestions.
June 24, 2005, 4:25 AM
OnlyMeat
[quote author=Vote Joe! link=topic=11946.msg117178#msg117178 date=1119585924]
[code]
        If Int(L / I) = (L / I) Then    '// If L divided by I rounded [/code]
[/quote]

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

[code]
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.
[/code]

Someone will have to confirm this ;)
June 24, 2005, 4:53 AM
Tontow
squair root x^(1/2)
third root x^(1/3)

June 24, 2005, 6:31 AM
TehUser
Not that it's especially important, but VB has a built-in square root function (Sqr).
June 24, 2005, 12:08 PM
kamakazie
[quote author=OnlyMeat link=topic=11946.msg117191#msg117191 date=1119588815]
You could possibly optimize that line by using the mod operator, which would mean you could eliminate 2 divisions.
[/quote]

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).
June 24, 2005, 10:30 PM
OnlyMeat
[quote author=dxoigmn link=topic=11946.msg117308#msg117308 date=1119652242]
Generally, if you're trying to find a remainder that means you're going to have to do division.
[/quote]

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 :-
[url]http://mathworld.wolfram.com/PrimalityTest.html[/url]

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 author=dxoigmn link=topic=11946.msg117308#msg117308 date=1119652242]
This implementation is a really naive way of doing a primality test and asymptotically slow.
[/quote]

[quote author=http://mathworld.wolfram.com/PrimalityTest.html]
(2002) unexpectedly discovered a polynomial time algorithm for primality testing that has asymptotic complexity.
[/quote]

Example 2.
[quote author=dxoigmn link=topic=11946.msg117308#msg117308 date=1119652242]
Faster primality tests are probablistic in nature (i.e. not deterministic)
[/quote]

[quote author=dxoigmn link=topic=11946.msg117308#msg117308 date=1119652242]
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.
[/quote]

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.

June 24, 2005, 11:54 PM
kamakazie
[quote author=OnlyMeat link=topic=11946.msg117316#msg117316 date=1119657241]
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 :-
[url]http://mathworld.wolfram.com/PrimalityTest.html[/url]

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.
[/quote]

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?
June 25, 2005, 12:00 AM
Myndfyr
[quote author=dxoigmn link=topic=11946.msg117317#msg117317 date=1119657611]
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?
[/quote]

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!  :)
June 25, 2005, 1:31 AM
OnlyMeat
[quote author=dxoigmn link=topic=11946.msg117317#msg117317 date=1119657611]
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?
[/quote]

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.
June 25, 2005, 5:36 AM
Adron
[quote author=OnlyMeat link=topic=11946.msg117359#msg117359 date=1119677813]
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.
[/quote]

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
June 25, 2005, 8:19 AM
dRAgoN
[quote author=OnlyMeat link=topic=11946.msg117359#msg117359 date=1119677813]
[quote author=dxoigmn link=topic=11946.msg117317#msg117317 date=1119657611]
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?
[/quote]

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.
[/quote]

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?..

[me=l)ragon]waits for his post to get removed ;p[/me]
June 25, 2005, 9:13 AM
UserLoser.
[code]
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
[/code]

Bad bad bad.  Grab a value from FreeFile, store it in some Integer and use it as the file number...!
June 25, 2005, 2:20 PM
JoeTheOdd
Wow you guys, now I remember why I don't share my code (not aimed at those who actually said something useful). I thought this might help someone, but aparently you don't want it.
June 28, 2005, 4:34 PM
UserLoser.
[quote author=Vote Joe! link=topic=11946.msg117897#msg117897 date=1119976487]
Wow you guys, now I remember why I don't share my code (not aimed at those who actually said something useful). I thought this might help someone, but aparently you don't want it.
[/quote]

Could help someone if you did some things better.  Such as grabbing a free file handle instead of always assuming one
June 28, 2005, 8:23 PM

Search