Skip to content

Instantly share code, notes, and snippets.

@hideki
Created October 10, 2010 22:53
Show Gist options
  • Save hideki/619662 to your computer and use it in GitHub Desktop.
Save hideki/619662 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
str = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
def palindrome(text):
length = len(text)
if length == 0 or length == 1:
return True
if(text[0] == text[length - 1]):
return palindrome(text[1:-1])
else:
return False
def main():
max =""
for i in range(0, len(str)):
for j in range(i, len(str)):
if palindrome(str[i:j+1]):
if len(max) < len(str[i:j+1]):
max = str[i:j+1]
print max
if __name__ == "__main__":
main()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sq = int(math.sqrt(n))
for i in range(3, sq+1, 2):
if n % i == 0:
return False
return True
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n-1) + fib(n-2)
def prime_divisors(n):
list = []
sq = int(n**0.5) + 1
for i in range(2, sq):
if is_prime(i) and n % i == 0:
list.append(i)
return list
def main():
X = 0
num = 227000
i = 0
while True:
f = fib(i)
#print f
if f > num:
if is_prime(f):
X = f
break
i += 1
print "step 1: %d" % X
print "step 2: %d" % sum(prime_divisors(X+1))
if __name__ == '__main__':
main()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
list = [3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99]
counter = 0
def find(start, end, n):
if end < 0:
return
X = list[end]
if n == X:
global counter
counter += 1
elif X < n:
find(start, end -1, n - X)
find(start, end -1, n)
def subsets(n):
idx = list.index(n)
find(0, idx - 1, n)
def main():
global counter
counter = 0
for n in list:
subsets(n)
print counter
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment