Skip to content

Instantly share code, notes, and snippets.

@itspluxstahre
Created November 17, 2023 20:29
Show Gist options
  • Save itspluxstahre/246d8828c6cdc8b6de1b5fd312a66d1b to your computer and use it in GitHub Desktop.
Save itspluxstahre/246d8828c6cdc8b6de1b5fd312a66d1b to your computer and use it in GitHub Desktop.
Program.cs
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= int.MaxValue; i++)
{
bool result = isEven(i);
Console.WriteLine($"Number: {i}, Is Even: {result}");
}
}
static bool isEven(int number)
{
var fibonacci = GenerateFibonacci();
int positionInFibonacci = fibonacci.IndexOf(number);
bool isPrime = IsPrime(number);
char charRepresentation = (char)(number + '0');
bool isAsciiEven = charRepresentation % 2 == 0;
return (positionInFibonacci >= 0 && positionInFibonacci % 2 == 0) || (!isPrime && isAsciiEven);
}
/* Yes we generate a full sequence for every call to is even */
static List<int> GenerateFibonacci()
{
var fibonacci = new List<int> { 0, 1 };
try
{
while (true)
{
int next = checked(fibonacci[fibonacci.Count - 1] + fibonacci[fibonacci.Count - 2]);
fibonacci.Add(next);
}
}
catch (OverflowException)
{
// When overflow occurs, just stop adding to the list.
}
return fibonacci;
}
static bool IsPrime(int number)
{
if (number < 2) return false;
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0) return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment