@@ -4,7 +4,7 @@ module Primes
4
4
5
5
using Base. Iterators: repeated
6
6
7
- import Base: start, next, done, eltype, iteratorsize, iteratoreltype
7
+ import Base: iterate, eltype, IteratorSize, IteratorEltype
8
8
using Base: BitSigned
9
9
using Base. Checked: checked_neg
10
10
@@ -675,36 +675,38 @@ struct NextPrimes{T<:Integer}
675
675
start:: T
676
676
end
677
677
678
- Base. start (np:: NextPrimes ) = np. start < 2 ? np. start : add (np. start, - 1 )
679
- Base. next (np:: NextPrimes , state) = (p = nextprime (add (state, 1 )); (p, p))
680
- Base. done (np:: NextPrimes , state) = false
678
+ function iterate (np:: NextPrimes ,
679
+ state = p = np. start < 2 ? np. start : add (np. start, - 1 )
680
+ )
681
+ p = nextprime (add (state, 1 ))
682
+ (p, p)
683
+ end
681
684
682
- Base . iteratorsize (:: Type{<:NextPrimes} ) = Base. IsInfinite ()
683
- Base . iteratoreltype (:: Type{<:NextPrimes} ) = Base. HasEltype () # default
685
+ IteratorSize (:: Type{<:NextPrimes} ) = Base. IsInfinite ()
686
+ IteratorEltype (:: Type{<:NextPrimes} ) = Base. HasEltype ()
684
687
685
- Base . eltype (:: Type{NextPrimes{T}} ) where {T} = T
688
+ eltype (:: Type{NextPrimes{T}} ) where {T} = T
686
689
687
690
"""
688
691
nextprimes(start::Integer)
689
692
690
- Returns an iterator over all primes greater than or equal to `start`,
693
+ Return an iterator over all primes greater than or equal to `start`,
691
694
in ascending order.
692
695
"""
693
696
nextprimes (start:: Integer ) = NextPrimes (start)
694
697
695
698
"""
696
699
nextprimes(T::Type=Int)
697
700
698
- Returns an iterator over all primes, with type `T`.
701
+ Return an iterator over all primes, with type `T`.
699
702
Equivalent to `nextprimes(T(1))`.
700
703
"""
701
- nextprimes (:: Type{T} ) where {T<: Integer } = nextprimes (one (T))
702
- nextprimes () = nextprimes (Int)
704
+ nextprimes (:: Type{T} = Int) where {T<: Integer } = nextprimes (one (T))
703
705
704
706
"""
705
707
nextprimes(start::Integer, n::Integer)
706
708
707
- Returns an array of the first `n` primes greater than or equal to `start`.
709
+ Return an array of the first `n` primes greater than or equal to `start`.
708
710
709
711
# Example
710
712
@@ -716,25 +718,34 @@ julia> nextprimes(10, 3)
716
718
17
717
719
```
718
720
"""
719
- nextprimes (start:: T , n:: Integer ) where {T<: Integer } = collect (T, take (nextprimes (start), n))
721
+ nextprimes (start:: T , n:: Integer ) where {T<: Integer } =
722
+ collect (T, Iterators. take (nextprimes (start), n))
720
723
721
724
struct PrevPrimes{T<: Integer }
722
725
start:: T
723
726
end
724
727
725
- start (np:: PrevPrimes ) = np. start+ one (np. start) # allow wrap-around
726
- next (np:: PrevPrimes , state) = (p = prevprime (state- one (state)); (p, p))
727
- done (np:: PrevPrimes , state) = state == 2
728
+ function iterate (np:: PrevPrimes ,
729
+ state = np. start+ one (np. start) # allow wrap-around
730
+ )
731
+ c = state- one (state)
732
+ if isone (c)
733
+ nothing
734
+ else
735
+ p = prevprime (c)
736
+ (p, p)
737
+ end
738
+ end
728
739
729
- iteratorsize (:: Type{<:PrevPrimes} ) = Base. SizeUnknown ()
730
- iteratoreltype (:: Type{<:PrevPrimes} ) = Base. HasEltype () # default
740
+ IteratorSize (:: Type{<:PrevPrimes} ) = Base. SizeUnknown ()
741
+ Iteratoreltype (:: Type{<:PrevPrimes} ) = Base. HasEltype ()
731
742
732
743
eltype (:: Type{PrevPrimes{T}} ) where {T} = T
733
744
734
745
"""
735
746
prevprimes(start::Integer)
736
747
737
- Returns an iterator over all primes less than or equal to `start`,
748
+ Return an iterator over all primes less than or equal to `start`,
738
749
in descending order.
739
750
740
751
# Example
@@ -753,8 +764,9 @@ prevprimes(start::Integer) = PrevPrimes(max(one(start), start))
753
764
"""
754
765
prevprimes(start::Integer, n::Integer)
755
766
756
- Returns an array of the first `n` primes less than or equal to `start`,
757
- in descending order.
767
+ Return an array of the first `n` primes less than or equal to `start`,
768
+ in descending order. When there are less than `n` primes less than or
769
+ equal to `start`, those primes are returned without an error.
758
770
759
771
# Example
760
772
@@ -764,8 +776,16 @@ julia> prevprimes(10, 3)
764
776
7
765
777
5
766
778
3
779
+
780
+ julia> prevprimes(10, 10)
781
+ 3-element Array{Int64,1}:
782
+ 7
783
+ 5
784
+ 3
785
+ 2
767
786
```
768
787
"""
769
- prevprimes (start:: T , n:: Integer ) where {T<: Integer } = collect (T, take (prevprimes (start, n)))
788
+ prevprimes (start:: T , n:: Integer ) where {T<: Integer } =
789
+ collect (T, Iterators. take (prevprimes (start), n))
770
790
771
791
end # module
0 commit comments