Sei sulla pagina 1di 3

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectEuler.MetodosComuns
{
public static class Comuns
{

public static int Fatorial(int n)


{
int retorno = 1;

if (n == 1 || n == 0)
return retorno;
else
{
while (n != 1)
{
retorno = retorno * n;
n--;
}
}
return retorno;

public static int SomaValoresLetras(string test)


{
int retorno = 0;
// trabalhar com tabela ASCII
foreach (var item in test)
{
retorno += (int)Convert.ToChar(item.ToString());

return retorno;
}

public static int ConvertCharParaInt(char a)


{
return (int)Convert.ToChar(a);

public static List<int> FindDivisors(int i)


{
List<int> lstDivisors = new List<int>();

for (int j = 1; j < i; j++)


{
if (i % j == 0)
{
if (!lstDivisors.Contains(j))
lstDivisors.Add(j);
}
}

return lstDivisors;
}

public static bool IsPandigitalNumber(long n)


{
string strNumber = n.ToString();
List<string> lstString = new List<string>();
List<string> lstNumbers = new List<string>();

for (int i = 0; i < strNumber.Length; i++)


{
lstNumbers.Add((i + 1).ToString());
if (lstString.Contains(strNumber[i].ToString()))
{
return false;

}
else
{
lstString.Add(strNumber[i].ToString());
}
}

foreach (var item in lstString)


{
if (!lstNumbers.Contains(item))
{
return false;
}
}

return true;

public static List<long> RetornaNumerosPrimos(long i)


{
List<long> numerosPrimos = new List<long>() { 2 };//, 3, 5, 7, 13, 11,
17, 23, 29, 37 };
bool isPrime = true;

if (i > numerosPrimos.Max())
{
for (long j = numerosPrimos.Max(); j < i; j++)
{
isPrime = true;
for (int k = 0; k < numerosPrimos.Count; k++)
{
if (j % numerosPrimos[k] == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
numerosPrimos.Add(j);
}
}
}
return numerosPrimos;
}

public static List<long> RetornaNumerosPrimos(long i, List<long>


primesNumbers)
{
List<long> numerosPrimos = primesNumbers;
bool isPrime = true;

if (i > numerosPrimos.Max())
{
for (long j = numerosPrimos.Max(); j < i; j++)
{
isPrime = true;
for (int k = 0; k < numerosPrimos.Count; k++)
{
if (j % numerosPrimos[k] == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
numerosPrimos.Add(j);
}
}
}
return numerosPrimos;
}

public static bool IsPrime(long i,List<long> numerosPrimos)


{
bool isPrime = true;

foreach (var item in numerosPrimos)


{
if (i % item == 0)
{
isPrime = false;
break;
}
}

return isPrime;
}
}
}

Potrebbero piacerti anche