# F(1) = 1 =1 # 3 3 =6 /5 # F(2) = ((1)+(2))+((1+2)) # 6 12 6 =24 /18 # 6 3 4 5 6 # F(3) = ((1)+(2)+(3))+((1+2)+(1+3)+(2+3))+((1+2+3)) # 10 30 30 10 =80 / 56 # F(4) = ((1)+(2)+(3)+(4))+((1+2)+(1+3)+(2+3) + (1+4)+(2+4)+(3+4) )+ ((1+2+3) + (1+2+4)+ (1+3+4) + (2+3+4)) +((1+2+3+4)) # # /5 20+10= /30 # # F(5) = ((1)+(2)+(3)+(4)+(5)) + ((1+2)+(1+3)+(2+3)+(1+4)+(2+4)+(3+4) + (5+1)+(5+2)+(5+3)+(5+4)) # 30+(1*3+2*3+3*3+4*3)=30+17+12=30+29= /59 # # + ((1+2+3)+(1+2+4)+(1+3+4)+(2+3+4) + (1+2+5) + (1+3+5) + (1+4+5) + (2+3+5) + (2+4+5) + (3+4+5)) from math import factorial levels = {0:0} def G(n): summary = 0 for i in range(1,n+1): summary+=F(i) return summary def F(s): levels[s] = combination_additional_sum(s) + levels[s-1] return levels[s] def combination_additional_sum(f_num): summary = 0 for l in range(1, f_num+1): print 'l:',l print 'summary:', summary if(l==1): print 'l==1, summary+=', f_num summary += f_num elif(l==f_num): sum_temp = sum(i for i in range(1,f_num+1)) print 'l==f_num, summary+=', sum_temp summary += sum_temp else: summary += comb_part_sum(f_num, l) return summary def comb_part_sum(f_num, comb_count): print 'comb_level_sum' print ' f_num',f_num print ' ccomb_count', comb_count summary = 0 #multiplier = comb_count-1 multiplier = factorial(f_num-1)/(factorial(comb_count-1)*factorial(f_num-comb_count))/(f_num-1) print ' multiplier', multiplier #summary += f_num #summary += sum(i for i in range(f_num)) for i in range(1, f_num): print ' i:',i print ' i*multiplier:',i*multiplier summary += i*multiplier summary +=f_num*(f_num-1) print ' comb_level_sum return=', summary return summary f_num =4 print f_num,':',combination_additional_sum(f_num) #print f_num,':',G(f_num) print levels
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question