Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2011 02:47
Show Gist options
  • Save anonymous/850382 to your computer and use it in GitHub Desktop.
Save anonymous/850382 to your computer and use it in GitHub Desktop.
static String getLongest(String s)
{
String longest = "";
for (int i = 1; i < s.Length; i++)
{
String first = s.Substring(0, i);
for (int j = 0; j < first.Length; j++)
{
String first_seg = first.Substring(j, first.Length - j);
if (i + 1 + first_seg.Length > s.Length)
continue;
for (int k = 0; k <= 1; k++)
{
String follow = s.Substring(i + k, first_seg.Length);
String joined = first_seg + (k == 1 ? s[i].ToString() : "") + follow;
if (joined == new String((char[])joined.Reverse().ToArray()))
{
if (joined.Length > longest.Length)
longest = joined;
}
}
}
}
return longest;
}
public static bool isPrime(Int32 num)
{
if ((num & 1) == 0)
{
if (num == 2)
return true;
else
return false;
}
for (int i = 3; (i * i) <= num; i += 2)
if ((num % i) == 0)
return false;
return num != 1;
}
public static Boolean isFibonacci(double num)
{
double test1 = Math.Sqrt(5 * num * num + 4);
double test2 = Math.Sqrt(5 * num * num - 4);
return Math.Round(test1) == test1 || Math.Round(test2) == test2;
}
static Int32 sumFactorize(Int32 num)
{
Int32 r = 0;
for (Int32 i = 1; i <= num; i++)
if (isPrime(i) && num % i == 0)
r += i;
return r;
}
static void recArray(int[] arr, int end, int max, int acum, ref int cont)
{
for (int i = end; i >= 0; i--)
{
int ev = acum + arr[i];
if (ev == max)
cont++;
else
if (ev > max)
continue;
else
if (i > 0)
recArray(arr, i - 1, max, ev, ref cont);
}
}
static int solveArray(int[] arr)
{
int cont = 0;
for (int i = arr.Length - 1; i > 1; i--)
recArray(arr, i - 1, arr[i], 0, ref cont);
return cont;
}
static void Main(string[] args)
{
String s = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
//Console.WriteLine(getLongest(s));
Int32 n = 227000;
while (true)
{
if (isPrime(n) && isFibonacci(n))
{
Console.WriteLine("n = " + n.ToString());
Console.WriteLine("r = " + sumFactorize(n + 1).ToString());
break;
}
n++;
}
int[] arr = { 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99 }; //{ 1, 2, 3, 4, 6 };//
Console.WriteLine(solveArray(arr));
Console.WriteLine("done");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment