-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1798d63d2db6f4967196adc671bbe8b3f |
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,23 @@ | ||
|
||
# Desafio 11 - Primos em PI! | ||
|
||
|
||
## pré-requisitos | ||
|
||
- [.NET SDK 8.0](https://dotnet.microsoft.com/download/dotnet/8.0) | ||
|
||
## compilar e executar | ||
|
||
1. **Clone o repositório:** | ||
```bash | ||
git clone https://github.com/flafmg/op-desafios.git | ||
cd op-desafios/desafio-11/flafmg/csharp | ||
``` | ||
2. **Compile o projeto:** | ||
```bash | ||
dotnet build | ||
``` | ||
3. **execute o programa** | ||
```bash | ||
dotnet run | ||
``` |
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,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
if (args.Length < 1) | ||
{ | ||
Console.WriteLine("usio correto: dotnet run <arquivo>"); | ||
return; | ||
} | ||
|
||
var fileName = args[0]; | ||
if (!File.Exists(fileName)) | ||
{ | ||
Console.WriteLine($"arquivo não encontrado: {fileName}"); | ||
return; | ||
} | ||
|
||
var primes = GeneratePrimesUpTo(9973); | ||
var biggestSequence = string.Empty; | ||
var decimalDigits = ReadDigitsFromFile(fileName); | ||
|
||
for (var i = 0; i < decimalDigits.Length; i++) | ||
{ | ||
var sequence = FindLongestPrimeSequence(primes, decimalDigits, i); | ||
if (sequence.Length > biggestSequence.Length) | ||
{ | ||
biggestSequence = sequence; | ||
} | ||
} | ||
|
||
Console.WriteLine(biggestSequence); | ||
} | ||
private static HashSet<int> GeneratePrimesUpTo(int limit) | ||
{ | ||
var primes = new HashSet<int>(); | ||
for (var num = 2; num <= limit; num++) | ||
{ | ||
if (IsPrime(num)) | ||
{ | ||
primes.Add(num); | ||
} | ||
} | ||
return primes; | ||
} | ||
private static bool IsPrime(int number) | ||
{ | ||
if (number < 2) return false; | ||
var sqrt = (int)Math.Sqrt(number); | ||
for (var i = 2; i <= sqrt; i++) | ||
{ | ||
if (number % i == 0) return false; | ||
} | ||
return true; | ||
} | ||
private static string ReadDigitsFromFile(string fileName) | ||
{ | ||
using var reader = new StreamReader(fileName); | ||
var content = reader.ReadToEnd(); | ||
|
||
var digits = new List<char>(); | ||
foreach (var ch in content) | ||
{ | ||
if (char.IsDigit(ch)) | ||
{ | ||
digits.Add(ch); | ||
} | ||
else if (ch == '.') | ||
{ | ||
digits.Clear(); | ||
} | ||
else if (!char.IsWhiteSpace(ch)) | ||
{ | ||
throw new Exception("arquivo contem caracteres invalidos"); | ||
} | ||
} | ||
|
||
return new string(digits.ToArray()); | ||
} | ||
private static string FindLongestPrimeSequence(HashSet<int> primes, string digits, int startIndex) | ||
{ | ||
var sequence = string.Empty; | ||
var longestSequence = string.Empty; | ||
|
||
for (var i = startIndex; i < Math.Min(startIndex + 4, digits.Length); i++) | ||
{ | ||
sequence += digits[i]; | ||
if (!int.TryParse(sequence, out var number) || !primes.Contains(number)) continue; | ||
|
||
var newSequence = FindLongestPrimeSequence(primes, digits, i + 1); | ||
var candidate = sequence + newSequence; | ||
if (candidate.Length > longestSequence.Length) | ||
{ | ||
longestSequence = candidate; | ||
} | ||
} | ||
|
||
return longestSequence; | ||
} | ||
} |
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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |