Created
October 5, 2011 00:54
-
-
Save einarwh/1263319 to your computer and use it in GitHub Desktop.
Sequence for generating prime number candidates using wheel factorization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WheelSequence : IEnumerable<int> | |
{ | |
private readonly int _startValue; | |
private readonly IEnumerable<int> _; | |
public WheelSequence(int startValue, | |
IEnumerable<int> skipSequence) | |
{ | |
_startValue = startValue; | |
_ = skipSequence; | |
} | |
public IEnumerator<int> GetEnumerator() | |
{ | |
yield return _startValue; | |
var wse = new WheelSequenceEnumerator(_startValue, | |
_.GetEnumerator()); | |
while (wse.MoveNext()) | |
{ | |
yield return wse.Current; | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
public class WheelSequenceEnumerator : IEnumerator<int> | |
{ | |
private readonly IEnumerator<int> _; | |
private int _value; | |
public WheelSequenceEnumerator(int startValue, | |
IEnumerator<int> skip) | |
{ | |
_value = startValue; | |
_ = skip; | |
} | |
public bool MoveNext() | |
{ | |
_.MoveNext(); | |
_value += _.Current; | |
return true; | |
} | |
public int Current | |
{ | |
get { return _value; } | |
} | |
object IEnumerator.Current | |
{ | |
get { return Current; } | |
} | |
public void Reset() | |
{ | |
throw new NotSupportedException(); | |
} | |
public void Dispose() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment