1. tt
Member
Join Date
Dec 1969
Posts
46

Hi there!<BR>Has anyone out there implemented the new Prime algorithm by Manindra Agrawal that is known to be extremly fast compared to for example my own lame code here:<BR><BR>Function PrimNr(Nr As Long)<BR>Dim lngLoop As Long<BR>Dim lngHalf As Long<BR>lngHalf = Nr / 2 + 1<BR>For lngLoop = 2 To lngHalf<BR>If (Nr Mod lngLoop) = 0 Then<BR>PrimNr = False<BR>Exit Function<BR>End If<BR>Next<BR>PrimNr = True<BR>End Function<BR><BR>He has made something that should work a lot better/faster with a few lines of code but either I&#039;m tired or stupid (hopfully the first).<BR><BR>Hes work and algorithm can be found here:<BR>http://www.cse.iitk.ac.in/news/primality.pdf<BR><BR>Thanks in advance // TT<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,854

try this

[code language="VBScript"]<BR>function IsPrime(ByRef LongNumber)<BR> Dim LongSquare<BR> Dim LongIndex<BR> IsPrime = False<BR> if LongNumber &#060; 2 Then Exit function<BR> if LongNumber Mod 2 = 0 Then Exit function<BR> LongSquare = Sqr(LongNumber)<BR> For LongIndex = 3 To LongSquare Step 2<BR> if LongNumber Mod LongIndex = 0 Then Exit function<BR> Next<BR> IsPrime = True<BR>End function <BR>[/code]

3. tt
Member
Join Date
Dec 1969
Posts
46

RE: try this

Thanks that was alot faster but what I understand the new released algorithm is way<BR>, way faster on huge numbers than mine and Yours are.<BR>Still it&#039;s intresting to see how You solved it...!<BR>Best // TT

4. tt
Member
Join Date
Dec 1969
Posts
46

RE: try this

Thanks that was alot faster but what I understand the new released algorithm is way<BR>, way faster on huge numbers than mine and Yours are.<BR>Still it&#039;s intresting to see how You solved it...!<BR>Best // TT

5. God
Senior Member
Join Date
Dec 1969
Posts
18,177

Why don't you..

.. specifically return False?<BR><BR>(Just out of curiousity..)

6. Senior Member
Join Date
Dec 1969
Posts
5,955

Isn't he? The code sets...

...IsPrime to False very near the beginning - just after the two DIMs.<BR><BR>Oliver.

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

Yeah, I see that now.

Thanks for pointing it out.<BR><BR>Which leads me back to my rant - functions should only be set at the bottom of the function.<BR><BR>:-)

8. Senior Member
Join Date
Dec 1969
Posts
5,955

Yeah, I know. That's why I prefer...

...JavaScript where you just do a "return false;". :-)<BR><BR>Oliver.

9. God
Senior Member
Join Date
Dec 1969
Posts
18,177

Heh. I still like..

.. to have my "return"s, in JavaScript (and C# and VB.NET), to be at the bottom of the function.<BR><BR>Just makes it easy to find.

10. Senior Member
Join Date
Dec 1969
Posts
5,955

Hm. I don't know. I guess we...

...could discuss this for ages - it&#039;s been a long-going discussion, I think. I generally do agree with you, but there are occasions where an early "return" is cleaner and still easy to read - but I lets agree to disagree.<BR><BR>:-)<BR><BR>Oliver.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•