#START=6561 #STOPP=19682 #SUMME=100 #BASIS="123456789" Structure TSumTerm sum.i ter.s EndStructure NewList Solutions.TSumTerm() NewMap SolCount.i() Dim op.s{1}(8) Dim b.s{1}(8) PokeS(@b(),#BASIS) Procedure StripTerm(*p_Term) If PeekS(*p_Term,1)="+" : PokeC(*p_Term,' ') : EndIf EndProcedure Procedure.s Triadisch(v) While v : r$=Str(v%3)+r$ : v/3 : Wend ProcedureReturn r$ EndProcedure Procedure.i Calc(t$) While Len(t$) x=Val(t$) : r+x If x<0 : s$=Str(x) : Else : s$="+"+Str(x) : EndIf t$=RemoveString(t$,s$,#PB_String_NoCase,1,1) Wend ProcedureReturn r EndProcedure For n=#START To #STOPP PokeS(@op(),Triadisch(n)) Term$="" For i=0 To 8 Select op(i) Case "0" : Term$+ b(i) Case "1" : Term$+"+"+b(i) Case "2" : Term$+"-"+b(i) EndSelect Next AddElement(Solutions()) : Solutions()\sum=Calc(Term$) : StripTerm(@Term$) : Solutions()\ter=Term$ Next SortStructuredList(Solutions(),#PB_Sort_Ascending,OffsetOf(TSumTerm\sum),TypeOf(TSumTerm\sum)) If OpenConsole() PrintN("Show all solutions that sum to 100:") ForEach Solutions() If Solutions()\sum=#SUMME : PrintN(#TAB$+Solutions()\ter) : EndIf SolCount(Str(Solutions()\sum))+1 Next ForEach SolCount() If SolCount()>MaxCount : MaxCount=SolCount() : MaxVal$=MapKey(SolCount()) : EndIf Next PrintN("Show the positve sum that has the maximum number of solutions:") PrintN(#TAB$+MaxVal$+" has "+Str(MaxCount)+" solutions") If LastElement(Solutions()) MaxVal=Solutions()\sum PrintN("Show the lowest positive number that can't be expressed:") For i=1 To MaxVal If SolCount(Str(i))=0 : PrintN(#TAB$+Str(i)) : Break : EndIf Next PrintN("Show the 10 highest numbers that can be expressed:") For i=1 To 10 PrintN(#TAB$+LSet(Str(Solutions()\sum),9)+" = "+Solutions()\ter) If Not PreviousElement(Solutions()) : Break : EndIf Next EndIf Input() EndIf