Skip to content

Commit

Permalink
Desafio 11 por @flafmg (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
flafmg authored Nov 29, 2024
1 parent 24368d5 commit 24bac64
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions desafio-11/flafmg/csharp/.valid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1798d63d2db6f4967196adc671bbe8b3f
23 changes: 23 additions & 0 deletions desafio-11/flafmg/csharp/README.MD
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
```
104 changes: 104 additions & 0 deletions desafio-11/flafmg/csharp/program.cs
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;
}
}
10 changes: 10 additions & 0 deletions desafio-11/flafmg/csharp/program.csproj
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>

0 comments on commit 24bac64

Please sign in to comment.