> #Weiji Zheng, 10/03/2020, Assignment 8 ; > #OK to post homework ; > ; > #Q1 ; > ; > #Code ; > CompsGFk:=proc(S,x,k) local s: > sort(expand((add(x^s, s in S))^k)): > end: > CompsGF:=proc(S,x) local s: > if not (type(S,set) and min(op(S))>0 ) then > print(`Bad input`): > RETURN(FAIL): > fi: > > factor(1/(1-add(x^s,s in S))): > > end: > CompsGFk({1,2,3},x,2); x^6+2*x^5+3*x^4+2*x^3+x^2 ; > #CompsGFk(S,x,k): inputs a FINITE set of INTEGERS (0 and negative integers are OK!) and a variable x, and > #a non-negative integer k, outputs the POLYNOMIAL (possibly Laurent polynomial, i.e. negative powers OK) > #whose coefficient of x^n is the EXACT number of words of length k, in the finite alphabet S > #that add-up to n. For example to get the generating function of all 4-letter words in {0,1} according to the > #sum (alias "number of 1s") try: > ; > #The number of 10-letter words in the alphabet {1,4,6} that add-up to 201 ; > CompsGFk({1,4,6},x,10) x^60+10*x^58+45*x^56+10*x^55+120*x^54+90*x^53+210*x^52+360*x^51+297*x^50+840*x^49+570*x^48+1260*x^47+1380*x^46+1380*x^45+2565*x^44+1680*x^43+3160*x^42+2880*x^41+2731*x^40+4290*x^39+2520*x^38+4210*x^37+3510*x^36+2772*x^35+4245*x^34+2100*x^33+3150*x^32+2640*x^31+1470*x^30+2520*x^29+1050*x^28+1260*x^27+1260*x^26+372*x^25+840*x^24+360*x^23+210*x^22+360*x^21+45*x^20+120*x^19+90*x^18+45*x^16+10*x^15+10*x^13+x^10 ; > #No number of 10-letter words in the alphabet {1,4,6} can add-up to 201 ; > ; > # The number of words in the alphabet {2,3,7} of ANY length, that add-up to 411 ; > # It's gonna be infinity of any length ; > ; > # The number of sequenes of any length whose entries are either 1 (mod 5) or 4 (mod 5) and that add up to 341 ; > # 1 (mod 5) = 1 ; > # 4 (mod 5) = 4 ; > CompsGF({1,4},x) -1/(x^4+x-1) ; > #I think the number is still infinity. ; > ; > #Q2 ; > ; > with(combinat,partition) [partition] ; > with(ListTools): > Pnk := proc(n,k) local P,a,b,S: > option remember: > > P := partition(n,k): > S := {}: > > for a from 1 to nops(P) do > > if FindMaximalElement(P[a])=k then > > S:=S union {sort(P[a],`>`)}: > fi: > od: > > S: > end: > #TEST ; > Pnk(5,2) {[2, 2, 1], [2, 1, 1, 1]} ; > > #Q3 > ; > pnk := proc(n,k) local num,SS: > option remember: > > SS := Pnk(n,k): > num :=numelems(SS): > num: > end: > pnk(5,3) 2 ; > pnk(5,4) 1 ; > pnk(5,2) 2 ; > ; > #Q4 ; > ; > pn := proc(n) local i,SSS,res: > option remember: > > SSS:={}: > > if n=0 then > RETURN(1): > else > for i from 1 to n do > SSS:=SSS union Pnk(n,i): > od: > fi: > res := numelems(SSS): > res: > end: > ; > pn(5) 7 ; > pn(0) 1 ; > seq(pn(n),n=0...30); 1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176, 231, 297, 385, 490, 627, 792, 1002, 1255, 1575, 1958, 2436, 3010, 3718, 4565, 5604 ; > #A number is A000041, denoting "a(n) is the number of partitions of n (the partition numbers)." ; > ; > #Q5 ; > ; > [seq(p(5*n+4) mod 5, n=1..50)] [p(9), p(14), p(19), p(24), p(29), p(34), p(39), p(44), p(49), p(54), p(59), p(64), p(69), p(74), p(79), p(84), p(89), p(94), p(99), p(104), p(109), p(114), p(119), p(124), p(129), p(134), p(139), p(144), p(149), p(154), p(159), p(164), p(169), p(174), p(179), p(184), p(189), p(194), p(199), p(204), p(209), p(214), p(219), p(224), p(229), p(234), p(239), p(244), p(249), p(254)] ; > [seq(p(7*n+5) mod 7, n=1..50)] [p(12), p(19), p(26), p(33), p(40), p(47), p(54), p(61), p(68), p(75), p(82), p(89), p(96), p(103), p(110), p(117), p(124), p(131), p(138), p(145), p(152), p(159), p(166), p(173), p(180), p(187), p(194), p(201), p(208), p(215), p(222), p(229), p(236), p(243), p(250), p(257), p(264), p(271), p(278), p(285), p(292), p(299), p(306), p(313), p(320), p(327), p(334), p(341), p(348), p(355)] ; > [seq(p(11*n+6) mod 11, n=1..50)] [p(17), p(28), p(39), p(50), p(61), p(72), p(83), p(94), p(105), p(116), p(127), p(138), p(149), p(160), p(171), p(182), p(193), p(204), p(215), p(226), p(237), p(248), p(259), p(270), p(281), p(292), p(303), p(314), p(325), p(336), p(347), p(358), p(369), p(380), p(391), p(402), p(413), p(424), p(435), p(446), p(457), p(468), p(479), p(490), p(501), p(512), p(523), p(534), p(545), p(556)] ; > #it takes too much time to computing ;