Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
657 B
3
Indexable
Never
List<int> fibSequence(int n) {
    List<int> sequence = new List<int> { 0, 1 };
    for (var i = 2; i <= n; i++)
    {
        sequence.Add(sequence[i - 1] + sequence[i - 2]);
    }
    return sequence;    
}

bool isPrime(int number) {
    if (number <= 1) return false;
    if (number == 2) return true;
    if (number % 2 == 0) return false;
    for (int i = 3; i <= Math.Sqrt(number); i += 2)
    {
        if (number % i == 0)
            return false;
    }
    return true;
}
var fibList = fibSequence(5);
IEnumerable<int> defList = fibList.Where(isPrime);

foreach (var number in defList)
{
    Console.WriteLine(number);
}