<div dir="ltr"><div dir="ltr">Cool. <div>I was just playing with some random crackpot ideas yesterday which involved primes.</div><div>This fix made a nice speed up :-)</div><div><br></div><div><div>   diff := OrderedCollection new.</div><div>   singular := 2.</div><div>   3 to: 5000000 do:[: i | i isPrime ifTrue:[ diff add: i - singular.</div><div><span style="white-space:pre">          </span>singular := i]].</div><div>  form := Form extent:8000@8000.<br></div><div>  pen := Pen newOnForm: form.</div><div>  pen up.</div><div>  pen goto: 100@6000.</div><div>  pen down.</div><div>  pen turn: 90.</div><div>  pen color: Color black.</div><div>  diff do:[:i | pen go: i. pen turn: 90].</div><div>  form writePNGfileNamed:  'Sketch.png'</div></div><div><br></div><div>Cheers,</div><div>Karl</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 24, 2018 at 11:09 PM <<a href="mailto:commits@source.squeak.org">commits@source.squeak.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Levente Uzonyi uploaded a new version of Kernel to project The Trunk:<br>
<a href="http://source.squeak.org/trunk/Kernel-ul.1192.mcz" rel="noreferrer" target="_blank">http://source.squeak.org/trunk/Kernel-ul.1192.mcz</a><br>
<br>
==================== Summary ====================<br>
<br>
Name: Kernel-ul.1192<br>
Author: ul<br>
Time: 24 October 2018, 10:50:43.875546 pm<br>
UUID: 72dfb776-3d77-4b84-9d69-d000e9a67ae1<br>
Ancestors: Kernel-eem.1191<br>
<br>
- improved Integer >> #isPrime's performance<br>
- slightly faster Categorizer >> #numberOfCategoryOfElement:<br>
<br>
=============== Diff against Kernel-eem.1191 ===============<br>
<br>
Item was changed:<br>
  ----- Method: Categorizer>>numberOfCategoryOfElement: (in category 'accessing') -----<br>
  numberOfCategoryOfElement: element <br>
        "Answer the index of the category with which the argument, element, is <br>
        associated."<br>
<br>
+       | categoryIndex elementIndex elementArraySize |<br>
-       | categoryIndex elementIndex |<br>
        categoryIndex := 1.<br>
        elementIndex := 0.<br>
+       elementArraySize := elementArray size.<br>
+       [(elementIndex := elementIndex + 1) <= elementArraySize]<br>
-       [(elementIndex := elementIndex + 1) <= elementArray size]<br>
                whileTrue: <br>
                        ["point to correct category"<br>
                        [elementIndex > (categoryStops at: categoryIndex)]<br>
                                whileTrue: [categoryIndex := categoryIndex + 1].<br>
                        "see if this is element"<br>
                        element = (elementArray at: elementIndex) ifTrue: [^categoryIndex]].<br>
        ^0!<br>
<br>
Item was changed:<br>
  ----- Method: Integer>>isPrime (in category 'testing') -----<br>
  isPrime<br>
        "Answer true if the receiver is a prime number. See isProbablyPrime for a probabilistic<br>
        implementation that is much faster for large integers, and that is correct to an extremely<br>
        high statistical level of confidence (effectively deterministic)."<br>
<br>
+       | probe step limit |<br>
+       self <= 3 ifTrue: [ ^self >= 2 ].<br>
+       self \\ 2 = 0 ifTrue: [ ^false ].<br>
+       self \\ 3 = 0 ifTrue: [ ^false ].<br>
+       self <= 1073741823 ifFalse: [ "1 << 30 - 1. For numbers larger than this (on 64-bit platforms) #isProbablyPrime is usually quicker."<br>
-       self <= 1 ifTrue: [ ^false ].<br>
-       self even ifTrue: [ ^self = 2].<br>
-       self <= 1073741823 ifFalse: [ "1 << 30 - 1. For numbers larger than this, the calculation takes longer than #isProbablyPrime when the receiver is a prime. The absolue turning point is about at 1 << 35 - 1."<br>
                ^self isProbablyPrime ].<br>
+       probe := 5.<br>
+       step := 2. "Step will oscillate between 2 and 4 because at this point self \\ 6 is either 1 or 5."<br>
+       limit := self sqrtFloor. "On 64-bit platforms this could be written as self asFloat sqrt truncated (+ 1), which is faster because it creates no intermediate objects. Knowing that self has at most 30 bits because of the check above, this value will never be larger than 32767."<br>
+       [ probe <= limit ] whileTrue: [<br>
+               self \\ probe = 0 ifTrue: [ ^false ].<br>
+               probe := probe + step.<br>
+               step := 6 - step ].<br>
-       3 to: self sqrtFloor by: 2 do: [ :each |<br>
-               self \\ each = 0 ifTrue: [ ^false ] ].<br>
        ^true!<br>
<br>
Item was added:<br>
+ ----- Method: LargeNegativeInteger>>isPrime (in category 'testing') -----<br>
+ isPrime<br>
+ <br>
+       ^false!<br>
<br>
<br>
</blockquote></div>