I was going to look at 200 f 300 and then try timespacex between your
f and other implementations of f, but I got a value error on
AddStartRangeIfNotThere

FYI,

-- 
Raul


On Sun, Sep 17, 2017 at 8:23 AM, Erling Hellenäs
<erl...@erlinghellenas.se> wrote:
> Hi all!
>
> Here is a version which does not use the p: verb. It is more useful as
> description of the algorithm. It is also probably less wasteful of computer
> resources.
>
> Cheers,
>
> Erling
>
> Prime=: [: -. [: +./0 = [ | ]
>
> (,2) Prime 3
>
> NB. 1
>
> 2 3 Prime 4
>
> NB. 0
>
> 2 3 Prime 5
>
> NB. 1
>
> 2 3 5 Prime 6
>
> NB. 0
>
> PrimesUntil=: 3 : 0
>
> primes=.i.0
>
> n=. 2 + i. y - 1
>
> for_i. n do.
>
> primes=.primes,(primes Prime i) # i
>
> end.
>
> primes
>
> )
>
> PrimesUntil 5
>
> NB. 2 3 5
>
> PrimesUntil 10
>
> NB. 2 3 5 7
>
> PrimesUntil 11
>
> NB. 2 3 5 7 11
>
> PrimesInRange=: 4 : 0
>
> n=. PrimesUntil y
>
> (n >: x)# n
>
> )
>
> 7 PrimesInRange 11
>
> NB. 7 11
>
> 7 PrimesInRange 10
>
> NB. 7
>
> 8 PrimesInRange 10
>
>
> NB. i.0
>
> AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]
>
> 3 AddStartRangeIfNotThere 3 5 7 11
>
> NB. 3 5 7 11
>
> 8 AddStartRangeIfNotThere i.0
>
> NB. 8
>
> AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [
>
> 11 AddEndRangeIfNotThere 3 5 7 11
>
> NB. 3 5 7 11
>
> 10 AddEndRangeIfNotThere 8
>
> NB. 8 10
>
> SequenceLengths=: 1 + (1 }. ]) - _1 }. ]
>
> SequenceLengths 3 5 7 11
>
> NB.3 3 5
>
> SequenceLengths 8 10
>
> NB. 3
>
> FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]
>
> 3 3 5 FirstInLongestSequence 3 5 7 11
>
> NB. 7
>
> 3 FirstInLongestSequence 8 10
>
> NB. 8
>
> SequenceIndices=: ] + [: i. [: >./ [
>
> 3 3 5 SequenceIndices 7
>
> NB. 7 8 9 10 11
>
> (,3) SequenceIndices 8
>
> NB. 8 9 10
>
> DropFirstIfPrime=: ((1 {. ]) e. [) }. ]
>
> 7 11 DropFirstIfPrime 7 8 9 10 11
>
> NB. 8 9 10 11
>
> (i.0) DropFirstIfPrime 8 9 10
>
> NB. 8 9 10
>
> DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]
>
> (,11)DropLastIfPrime 8 9 10 11
>
> NB. 8 9 10
>
> (i.0)DropLastIfPrime 8 9 10
>
> f=: 4 : 0
>
> NB. Find the longest sequence of non-primes in a range
>
> NB. x and y are the range limits
>
> primesInRange =. x PrimesInRange y
>
> NB. Add range limits if they are not primes and then already there
>
> rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere
> primesInRange
>
> NB. The lengths of the possible sequences, including both sequence limits
>
> sequenceLengths =. SequenceLengths rangeLimits
>
> NB. The first element of the longest sequence
>
> firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits
>
> NB. The longest range including it's both limits
>
> longestRange =. sequenceLengths SequenceIndices firstInLongestSequence
>
> NB. Drop the range limits if they are primes
>
> primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange
>
> )
>
>
> s=: 10 f 100
>
> s
>
>>./90 91 92 93 94 95 96 = s
>
> s=: 1 f 2
>
>>./s = 1
>
> s=: 2 f 3
>
> 0 = $ s
>
> s=: 8 f 9
>
>>./s = 8 9
>
> s=: 7 f 8
>
>>./s = 8
>
> s=: 10 f 11
>
>>./s = 10
>
> s=: 7 f 11
>
>>./s = 8 9 10
>
> s=: 200 f 300
>
> s
>
>>./0 = 1 p: s
>
> 1 = 1 p: _1 + 1 {. s
>
> 1 = 1 p: 1 + _1 {. s
>
> s=: 2000 f 3000
>
> s
>
>>./0 = 1 p: s
>
> 1 = 1 p: _1 + 1 {. s
>
> 1 = 1 p: 1 + _1 {. s
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to