#! /usr/bin/python
# -*- coding: utf-8 -*-
def if_even(n):
return n / 2
def if_odd(n):
return n * 3 + 1
def collatz(n):
m = if_odd(n) # 初回
while(True):
if m == 1:
return False
elif m == n:
return True
elif m % 2 == 0:
m = if_even(m)
else:
m = if_odd(m)
def main():
collatz_nums = []
for n in xrange(1, 10001):
if collatz(n):
collatz_nums.append(n)
print '最初の数に戻る数:', collatz_nums
print 'その個数:', len(collatz_nums)
if __name__ == '__main__':
main()
#! /usr/bin/python
# -*- coding: utf-8 -*-
import itertools
def exchange(money, max):
""" money を指定枚数 max 以内で両替できるパターンを返す """
coins = [500, 100, 50, 10]
candidate_patterns = []
for i in xrange(2, max+1):
candidate_patterns.extend(list(itertools.combinations_with_replacement(coins, i)))
answer_patterns = []
for i, pattern in enumerate(candidate_patterns):
if sum(pattern) == money:
answer_patterns.append(pattern)
print '+ 組み合わせ一覧'
for pattern in answer_patterns:
print pattern
print '\n組み合わせの数 =', len(answer_patterns)
def main():
exchange(1000, 15)
if __name__ == '__main__':
main()
#! /usr/bin/python
# -*- coding: utf-8 -*-
class Bar:
def __init__(self, n, done=False):
self.len = n
self.done = done # その回で既に切り分けたかどうかのフラグ
def calc_min_times(n, m):
bar_list = [Bar(n)]
count = 0
print 'Now state:', count, show_state(bar_list)
while not is_finished(bar_list):
count += 1
reset_done(bar_list)
for i in xrange(m):
for j, bar in enumerate(bar_list):
if bar.len != 1 and bar.done is False:
len_tmp = bar.len
bar_list[j].len = len_tmp / 2
bar_list[j].done = True
bar_list.append(Bar(len_tmp - len_tmp / 2, True))
bar_list = sorted(bar_list, key=lambda x: x.len)
bar_list.reverse()
break
print 'Now state:', count, show_state(bar_list)
return count
def show_state(bar_list):
states = []
for bar in bar_list:
states.append(bar.len)
return states
def is_finished(bar_list):
for bar in bar_list:
if bar.len != 1:
return False
return True
def reset_done(bar_list):
for bar in bar_list:
bar.done = False
if __name__ == '__main__':
q1 = calc_min_times(20, 3)
q2 = calc_min_times(100, 5)
print ''
print 'Q1 Answer:', q1
print 'Q2 Answer:', q2
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
if __name__ == '__main__':
ans = []
kigo = ['+', '-', '*', '/', '']
for a in xrange(0,10):
for b in xrange(0,10):
for c in xrange(0,10):
for d in xrange(0,10):
for k1 in kigo:
for k2 in kigo:
for k3 in kigo:
equation = str(a)+k1+str(b)+k2+str(c)+k3+str(d)
num_rev = str(d)+str(c)+str(b)+str(a)
print equation
e = None
try:
if k1 == '' and k2 == '' and k3 == '':
continue
if str(eval(equation)) == num_rev:
ans.append(equation)
except:
pass
print ans