-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed number parsing to be region agnostic (always use
.
to separat…
…e decimals)
- Loading branch information
1 parent
837322c
commit 3faec2e
Showing
7 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Yolol.Execution; | ||
|
||
namespace YololEmulator.Tests.Ladder | ||
{ | ||
[TestClass] | ||
public class PrimeCountingFunction | ||
: BaseGenerator | ||
{ | ||
private readonly Dictionary<int, bool> _cache = new(); | ||
|
||
[TestMethod] | ||
public void Generate() | ||
{ | ||
Run(34923, 10000, false, Generator.ScoreMode.BasicScoring); | ||
} | ||
|
||
protected override bool GenerateCase(Random random, int index, Dictionary<string, Value> inputs, Dictionary<string, Value> outputs) | ||
{ | ||
var i = index + 1; | ||
inputs.Add("i", i); | ||
outputs.Add("o", PrimeCount(i)); | ||
return true; | ||
} | ||
|
||
private int PrimeCount(int n) | ||
{ | ||
var count = n >= 2 ? 1 : 0; | ||
for (var i = 3; i < n; i += 2) | ||
if (IsOddIntPrime(i)) | ||
count++; | ||
return count; | ||
} | ||
|
||
private bool IsOddIntPrime(int number) | ||
{ | ||
if (number <= 1) | ||
return false; | ||
if (_cache.TryGetValue(number, out var cached)) | ||
return cached; | ||
|
||
var boundary = (int)Math.Floor(Math.Sqrt(number)); | ||
|
||
for (var i = 3; i <= boundary; i += 2) | ||
{ | ||
if (number % i == 0) | ||
{ | ||
_cache[number] = false; | ||
return false; | ||
} | ||
} | ||
|
||
_cache[number] = true; | ||
return true; | ||
} | ||
} | ||
} |
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