static void Main(string[] args) { var s = new Sieve(15L); s.Primes.ForEach(Console.WriteLine); Console.ReadKey(); }
public async Task <IActionResult> GetTransactions([FromQuery] DateRangeSieveModel model) { bool search = !string.IsNullOrWhiteSpace(model.SearchQuery); DateRange range = new DateRange(model.Start, model.End); var filter = range.Includes <Transaction>(); if (search) { string json = await System.IO.File.ReadAllTextAsync(Path.Combine(Core.DATA_DIR, "paymentChannels.json")); var channels = JsonConvert.DeserializeObject <List <PaymentChannel> >(json); foreach (var channel in channels) { filter = filter.Or(t => t.PaymentChannelId == channel.Id); } } var transactions = await DataContext.Store.GetAllAsync(filter);//await (await DataContext.Store.GetOneAsync<User>(u => u.IsPlatform)).Wallet.GetTransactions(filter); var data = Sieve.Apply(model, Mapper.Map <IEnumerable <BackOfficeTransactionViewModel> >(transactions).AsQueryable()); return(Ok(data)); }
public void ExampleTest() { Sieve s = new Sieve(100); Assert.IsTrue(s[19]); Assert.IsFalse(s[91]); }
static void Main(string[] args) { var primes = Sieve.GetSieve(target); long sum = primes.Sum(); Console.WriteLine(sum); }
public void ReturnsAllPrimeNumbersForCasesFormThreeToThirty() { // Arrange var algorithm = new Sieve(); // Act | Assert Assert.AreEqual(new[] { 2, 3 }, algorithm.Calculate(3)); Assert.AreEqual(new[] { 2, 3, 5 }, algorithm.Calculate(5)); Assert.AreEqual(new[] { 2, 3, 5 }, algorithm.Calculate(6)); Assert.AreEqual(new[] { 2, 3, 5, 7 }, algorithm.Calculate(7)); Assert.AreEqual(new[] { 2, 3, 5, 7 }, algorithm.Calculate(8)); Assert.AreEqual(new[] { 2, 3, 5, 7 }, algorithm.Calculate(9)); Assert.AreEqual(new[] { 2, 3, 5, 7 }, algorithm.Calculate(10)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11 }, algorithm.Calculate(11)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11 }, algorithm.Calculate(12)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13 }, algorithm.Calculate(13)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13 }, algorithm.Calculate(14)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13 }, algorithm.Calculate(15)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13 }, algorithm.Calculate(16)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17 }, algorithm.Calculate(17)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17 }, algorithm.Calculate(18)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19 }, algorithm.Calculate(19)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19 }, algorithm.Calculate(20)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19 }, algorithm.Calculate(21)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19 }, algorithm.Calculate(22)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(23)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(24)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(25)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(26)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(27)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }, algorithm.Calculate(28)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, algorithm.Calculate(29)); Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, algorithm.Calculate(30)); }
private BigInteger FindFactor(BigInteger val) { Sieve sieve = new Sieve(2000); BigInteger factor = val; for (int i = 0; i < sieve.Count; i++) { while (factor > 1 && factor % sieve[i] == 0) { factor /= sieve[i]; } } BigInteger root = val.Sqrt(); root = root.Sqrt(); root++; root = root * root; if (factor > root && BigInteger.IsProbablePrime(rand, factor, 50)) { return(factor); } return(-1); }
/// <summary> /// /// </summary> /// <typeparam name="TResource"></typeparam> /// <param name="query"></param> /// <returns></returns> public ParseResult <Sieve <TResource> > TryParse <TResource>(string query) where TResource : class { var model = new Sieve <TResource>(); foreach (var node in query.Split(OPERATION_DELIMITER)) { if (node.StartsWith(FILTER, StringComparison.OrdinalIgnoreCase)) { var result = TryParseFilter <TResource>(node.Substring(FILTER.Length)); if (result.Successful) { model.Filter = result.Result; } else { return(new ParseResult <Sieve <TResource> >(result.Errors)); } } else if (node.StartsWith(SORT, StringComparison.OrdinalIgnoreCase)) { var result = TryParseSort <TResource>(node.Substring(SORT.Length)); if (result.Successful) { model.Sort = result.Result; } else { return(new ParseResult <Sieve <TResource> >(result.Errors)); } } else if (node.StartsWith(SKIP, StringComparison.OrdinalIgnoreCase)) { var result = TryParseConst(node.Substring(SKIP.Length)); if (result.Successful) { model.Skip = result.Result ?? model.Skip; } else { return(new ParseResult <Sieve <TResource> >(result.Errors)); } } else if (node.StartsWith(TAKE, StringComparison.OrdinalIgnoreCase)) { var result = TryParseConst(node.Substring(TAKE.Length)); if (result.Successful) { model.Take = result.Result ?? model.Take; } else { return(new ParseResult <Sieve <TResource> >(result.Errors)); } } } return(new ParseResult <Sieve <TResource> >(model)); }
public void InstanceIsDefined() { // Arrange var algorithm = new Sieve(); // Act | Assert Assert.IsInstanceOf(typeof(Sieve), algorithm); }
public void InstanceIsDefined() { // Arrange var instance = new Sieve(); // Act | Assert Assert.IsInstanceOf <Sieve>(instance); }
static void Main(string[] args) { var product = Sieve.GetSieve(target) .Select((prime) => (long)Math.Pow(prime, Math.Floor(Math.Log(target, prime)))) .Aggregate((subProduct, n) => subProduct * n); Console.WriteLine(product); }
public static void Main() { Console.Write("Input a number up to which you want to see the prime nunbers:"); int n = int.Parse(Console.ReadLine()); Sieve s = new Sieve(n); s.Process(); s.PrintPrimes(); }
public static void DoTest() { long[] Result = new long[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 }; Assert.AreEqual(Result, Sieve.Generate(40)); }
public void FindsAllPrimeNumbersForHundredInputCase() { // Arrange var algorithm = new Sieve(); // Act | Assert Assert.AreEqual(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }, algorithm.Calculate(100)); }
public void SieveOfErasthonesTest() { ICollection result = (ICollection)Sieve.Calculate(30); ICollection expected = (ICollection) new List <int>() { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, }; CollectionAssert.AreEqual(expected, result); }
/// <summary> /// Evaluate the primes in the NumberTextBox. /// </summary> public void Evaluate() { StringBuilder primesText = new StringBuilder(); IEnumerable <int> primes = Sieve.Calculate(NumberTextBox); foreach (int prime in primes) { primesText.Append(prime).Append(' '); } PrimesTextBlock = primesText.ToString(); }
public static void Main(string[] args) { var s = new Sieve(2); var i = 2; Console.WriteLine("2"); while (i < 10) { i += 1; s.Test(i); } }
public void TestValidNSet() { Sieve s = new Sieve(1); int[] validInputs = { 2, 3, 100, 5, 20, 1 }; foreach (var i in validInputs) { try{ s.N = i; } catch (SieveException e) { Assert.Fail($"s.N = {i} threw an exception!\n{e.StackTrace}"); } } }
static void Main() { string line; int maxval; Console.Write("Number of primes from 2 to "); line = Console.ReadLine(); maxval = Convert.ToInt32(line); Sieve sv = new Sieve(); sv.SyncCall(maxval); Console.WriteLine("Press return to exit ..."); Console.ReadLine(); }
public void TestPrimes() { Sieve s = new Sieve(100); int[] primes = { 2, 3, 5, 7, 11, 13 }; foreach (var i in primes) { bool result = s[i]; Assert.IsTrue( result, $"Expected for {i}: true; Actual: {result}" ); } }
public void TestNotPrimes() { Sieve s = new Sieve(100); int[] notPrimes = { 1, 4, 6, 8, 9, 10, 12 }; foreach (var i in notPrimes) { bool result = s[i]; Assert.IsFalse( result, $"Expected for {i}: false; Actual: {result}" ); } }
public static bool IsPrime(long n) { if (n < s_max) { return(s_primes.BinarySearch(n) > 0); } var sqrt = Math.Sqrt(n); if (s_primes.Count == 0 || s_max * s_max < n) { s_primes = Sieve.UpTo <List <long> >((long)sqrt * 2); } return(!(n < 2) && (n == 2 || IsPrimeHelper(n, 1, sqrt))); }
public void Finds_primes_up_to_1000() { Assert.Equal(new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 }, Sieve.Primes(1000)); }
public void Test(int n) { if (_prime * (n / _prime) == n) { } else if (_next == null) { Console.WriteLine(n); _next = new Sieve(n); } else { _next.Test(n); } }
public void TestOutOfBounds() { Sieve s = new Sieve(100); int[] invalidInputs = { -1, -3, 101, 1000 }; foreach (var i in invalidInputs) { //try{ Assert.ThrowsException <SieveException>( () => s[i], $"s[{i}] did not throw SieveException!" ); //} catch (AssertFailedException e){ // Debug.Write(e.Message + "\n"); //} } }
public void TestInvalidInitialisations() { Sieve s; int[] invalidInputs = { 0, -1, -100 }; foreach (var i in invalidInputs) { //try{ Assert.ThrowsException <SieveException>( () => s = new Sieve(i), $"s = new Sieve({i}) did not throw exception!" ); //} catch (AssertFailedException e){ //Debug.Write(e.Message + "\n"); //} } }
public void TestInvalidNSet() { Sieve s = new Sieve(100); int[] invalidInputs = { 0, -1, -100 }; foreach (var i in invalidInputs) { //try{ Assert.ThrowsException <SieveException>( () => s.N = i, $"s.N = {i} did not throw exception!" ); //} catch (AssertFailedException e){ //Debug.WriteLine(e.Message + "\n"); //} } }
public static long[] Factor(decimal n) { long[] pfactors = Sieve.Generate((long)Math.Sqrt(Convert.ToDouble(n))); List <long> factors = new List <long>(); foreach (var item in pfactors) { while (n % item == 0) { factors.Add(item); n /= item; } } return(factors.ToArray()); }
public static bool Pocklington(ulong n) { ulong[] primes = Sieve.Standard((ulong)System.Math.Log(n - 1)).Cast <ulong>().ToArray(); ulong f = n - 1, sqrt = (ulong)System.Math.Sqrt(n); foreach (ulong prime in primes) { if (f / prime < sqrt) { goto g; } while (f % prime == 0) { if (f / prime < sqrt) { goto g; } f /= prime; } } g: var factors = Factorise.Standard(f, (z) => (ulong)Factoring.Special.TrialDivision((int)z)); for (uint i = 2; i < n; i++) { if (Power.BinaryMod(i, n - 1, n) == 1) { bool isPrime = true; foreach (uint factor in factors) { if (GCD.Standard((uint)Power.Binary(i, (n - 1) / factor) - 1, n) != 1) { isPrime = false; break; } } if (isPrime) { return(true); } } } return(false); }
static void Main() { string line; int maxval, t = 1; Console.Write("Number of primes from 2 to "); line = Console.ReadLine(); maxval = Convert.ToInt32(line); Sieve sv = new Sieve(); sv.AsyncCall(maxval); Console.WriteLine("Press return to exit ..."); while (!Console.KeyAvailable) { Thread.Sleep(1000); Console.Write("..{0}", t++); } Console.ReadKey(true); }
public static IResultSubject[] Filter(IList <ISubject> candidates, string query, StringScorerOptions options = null) { if (string.IsNullOrEmpty(query)) { return(new IResultSubject[0]); } if (candidates == null || !candidates.Any()) { return(new IResultSubject[0]); } options = options ?? new StringScorerOptions(); options.Init(query); var result = Sieve.FilterCandidates(candidates.ToList().ConvertAll(Convert).ToArray(), query, options) .ToList(); return(result.ConvertAll(Convert).ToArray()); }
public void TestFirstPrimes() { var sieve = new Sieve(); var firstPrimes = new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 }; Assert.AreEqual(firstPrimes, sieve.Take(firstPrimes.Length)); }
public void SetUp() { Sieve = new Sieve(Max); }
public static IWorker buildStdioLib( TextReader inStream, TextWriter outStream, TextWriter logStream ) { Sieve sieve = new Sieve(null); //println function IScidentre ws = sieve.reserveScidentre( true, new Identifier("println"), ScidentreCategory.OVERLOAD); ws.type = new NType(); ws.assign( toClientFunction( new Function_Native( new ParameterImpl[] { new ParameterImpl( Direction.IN, stdn_any, new Identifier("text"), null) }, null, delegate(IScope args) { IWorker arg = GE.evalIdent(args, "text"); outStream.WriteLine( arg is Null ? "null" : Bridge.toNativeString(arg) ); return new Null(); }, null))); //get_exit_status function IScidentre ws2 = sieve.reserveScidentre( true, new Identifier("get exit status"), ScidentreCategory.OVERLOAD); ws2.type = new NType(); ws2.assign( toClientFunction( new Function_Native( new ParameterImpl[] {}, null, delegate(IScope args) { return toClientInteger(0); }, null))); return LibraryWrapper.wrap(sieve); }
public void TestThousandPrimes() { var sieve = new Sieve(); Assert.AreEqual(precomputedPrimes.Take(1000), sieve.Take(1000)); }