<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>G2048</title>
		<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048</link>
		<description>Posts in the discussion thread &quot;G2048&quot; - Optimize this program</description>
				<copyright></copyright>
		<lastBuildDate>Sun, 07 Jun 2026 17:58:20 +0000</lastBuildDate>
		
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364922</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364922</link>
				<description></description>
				<pubDate>Thu, 27 Aug 2015 05:16:44 +0000</pubDate>
								<wikidot:authorUserId>1873534</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364905</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364905</link>
				<description></description>
				<pubDate>Thu, 27 Aug 2015 03:30:46 +0000</pubDate>
				<wikidot:authorName>kgmstwo</wikidot:authorName>				<wikidot:authorUserId>2205570</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>I understood you the first time. As the page on <a href="http://tibasicdev.wikidot.com/memory-leaks">memory-leaks</a> says, a memory leak can cause, but is not the same as, an ERR:MEMORY; and there is also an actual disadvantage to writing programs like you example &quot;not a memory leak&quot; programs. A memory leak is when you load up the stack with pointers that your program will never use, which you rely on the termination of the program to clear off the stack. Why should you worry about a finite (or &quot;fixed&quot; as you called it) memory leak? In addition to reducing the amount of memory the program has to allocate variables, due to a bug this will apparently also slow down the program. That's not to say that loading up on the stack is bad, just like you said, sometimes it is needed. The whole reason for the name &quot;memory leak&quot; is that you might load up the stack with things that you don't need, and it's just left their taking up memory until the program exits.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364898</guid>
				<title>Re: Shorter</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364898</link>
				<description></description>
				<pubDate>Thu, 27 Aug 2015 03:05:54 +0000</pubDate>
				<wikidot:authorName>kgmstwo</wikidot:authorName>				<wikidot:authorUserId>2205570</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <div class="code"> <pre><code>PROGRAM:G2048 Delvar θ 0identity(4 → [A] SetupEditor G2048 Menu(&quot;2048&quot;,&quot;LOAD&quot;,L,&quot;NEW&quot;,N Lbl L ‾1 → θ List►matr(seq(⌊G2048(4A-3,A,1,4),seq(⌊G2048(4A-2,A,1,4),seq(⌊G2048(4A-1,A,1,4),seq(⌊G2048(4A,A,1,4)→ [A] Lbl N AxesOff FnOff ZStandard ZInteger θ+1 → θ Repeat K=45 If θ:Then Repeat not([A](X,Y randInt(1,4 → X randInt(1,4 → Y End 2+2(rand&gt;.9 → [A](X,Y End ClrDraw For(A,‾36,36,18 5A/6 Line(A,‾30,A,30 Line(‾36,Ans,36,Ans:End For(A,1,4 For(B,1,4 Text(15A-10,18B-5,&quot; &quot; \\15sp [A](A,B If Ans Text(15A-10,18B+1-2int(log(Ans)),Ans End:End Repeat Ans Text(‾1,0,90,&quot; &quot; \\1sp getKey→K End Delvar θ For(X,1,4 seq([A](B,X(K=25 or K=34)+B(K=24 or K=36),B(K=25 or K=34)+X(K=24 or K=36),1,4 → L₁ For(A,2,dim(L₁)-2dim(L₁)(not(sum(L₁ For(B,A,2,‾1+2not(L₁(A) L₁(B)=L₁(B-1) or not(L₁(B-1→Z not(Z)L₁(B-1)+Z(L₁(B-1)+L₁(B → L₁(B-1 not(Z)L1(B → L1(B not(Z)θ+Z → θ ZB+not(Z)2 → B End:End For(B,1,4 L₁(B → [A](B(K=25)+X(K=26)+(5-B)(K=34)+X(K=24),X(K=25)+(5-B)(K=26)+X(K=34)+B(K=24) End:End:End seq([A](int((A+3)/4),1+4fpart((A-1)/4)),A,1,16) → lG2048</code></pre></div> <p>Nice! Inlining also lets you get rid of the surrounding for loop over L.<br /> I piecewised out the conditional on sum(L₁ and L₁(A into their respective for loops, and the assignments that were in the subprogram. You could also substitute Z if you wanted to eliminate one more line.<br /> I also replaced characters using the ones from the char box. The ~ for the negative sign had been really bothering me.</p> <p>EDIT:</p> <p>I also replaced</p> <div class="code"> <pre><code>For(A,1,4) For(B,1,4) ⌊G2048(4A+B-4 → [A](A,B End:End</code></pre></div> <p>with</p> <div class="code"> <pre><code>List►matr(seq(⌊G2048(4A-3,A,1,4),seq(⌊G2048(4A-2,A,1,4),seq(⌊G2048(4A-1,A,1,4),seq(⌊G2048(4A,A,1,4)→ [A]</code></pre></div> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364877</guid>
				<title>Re: Shorter</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364877</link>
				<description></description>
				<pubDate>Thu, 27 Aug 2015 02:02:41 +0000</pubDate>
				<wikidot:authorName>lirtosiast</wikidot:authorName>				<wikidot:authorUserId>2005367</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Nice golf, but you can now also eliminate the subroutine calling code by writing it inline.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364847</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364847</link>
				<description></description>
				<pubDate>Thu, 27 Aug 2015 00:01:12 +0000</pubDate>
								<wikidot:authorUserId>1873534</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364622</guid>
				<title>Shorter</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364622</link>
				<description></description>
				<pubDate>Wed, 26 Aug 2015 16:14:43 +0000</pubDate>
				<wikidot:authorName>kgmstwo</wikidot:authorName>				<wikidot:authorUserId>2205570</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <div class="code"> <pre><code>PROGRAM:G2048 Goto Ø Lbl M If not(sum(L1 End For(A,2,dim(L1 If L1(A:Then For(B,A,2,~1 If L1(B)=L1(B-1) or not(L1(B-1 Then L1(B-1)+L1(B → L1(B-1 0 → L1(B 1 → Ø Else 2 → B End:End:End:End:End Lbl Ø Delvar Ø 0identity(4 → [A] SetupEditor G2048 Menu(&quot;2048&quot;,&quot;LOAD&quot;,L,&quot;NEW&quot;,N Lbl L ~1 → Ø For(A,1,4) For(B,1,4) lG2048(4A+B-4 → [A](A,B End:End Lbl N AxesOff FnOff ZStandard ZInteger Ø+1 → Ø Repeat K=45 If Ø:Then Repeat not([A](X,Y randInt(1,4 → X randInt(1,4 → Y End 2+2(rand&gt;.9 → [A](X,Y End ClrDraw For(A,~36,36,18 5A/6 Line(A,~30,A,30 Line(~36,Ans,36,Ans:End For(A,1,4 For(B,1,4 Text(15A-10,18B-5,&quot; &quot; \\15sp [A](A,B If Ans Text(15A-10,18B+1-2int(log(Ans)),Ans End:End Repeat Ans Text(~1,0,90,&quot; &quot; \\1sp getKey→K End Delvar Ø For(X,1,4 seq([A](B,X(K=25 or K=34)+B(K=24 or K=36),B(K=25 or K=34)+X(K=24 or K=36),1,4 → L1 For(L,~1,0 If L:Goto M End:For(B,1,4 L1(B → [A](B(K=25)+X(K=26)+(5-B)(K=34)+X(K=24),X(K=25)+(5-B)(K=26)+X(K=34)+B(K=24) End:End:End seq([A](int((A+3)/4),1+4fpart((A-1)/4)),A,1,16) → lG2048</code></pre></div> <p>I used <a href="http://tibasicdev.wikidot.com/piecewise-expressions">piecewise-expressions</a> to combine the code for the four arrow key presses.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2364610</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2364610</link>
				<description></description>
				<pubDate>Wed, 26 Aug 2015 15:50:14 +0000</pubDate>
				<wikidot:authorName>kgmstwo</wikidot:authorName>				<wikidot:authorUserId>2205570</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>This is somewhat misleading. Your example program that &quot;does not cause a memory leak&quot; actually does cause a memory leak- like you said, the stack is at 100. That is the leaked memory, which is inaccessible for the remainder of the program and could also slow the calculator down for the remainder of the program according to the <a href="http://tibasicdev.wikidot.com/memory-leaks">wiki</a>. Causing the stack to &quot;go to infinity&quot; will cause a <a href="http://tibasicdev.wikidot.com/errors#memory">memory error</a>.</p> <p>Michael's method of internal subprograms is a version of the &quot;branching out of loops&quot; section from the <a href="http://tibasicdev.wikidot.com/subprograms">subprograms</a> page, which is derived from Brad Wentz's <a href="http://tibasicdev.wikidot.com/local--files/downloads/looptrick.zip">looptrick</a>. It's really cool and new to me- I'd always just avoided Goto like the plague!</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363683</guid>
				<title>(no title)</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363683</link>
				<description></description>
				<pubDate>Tue, 25 Aug 2015 03:55:31 +0000</pubDate>
				<wikidot:authorName>Michael</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I structured it this way, with internal subprograms, because to me, one large program is better than several smaller programs.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363584</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363584</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 23:26:37 +0000</pubDate>
				<wikidot:authorName>Michael2_3B</wikidot:authorName>				<wikidot:authorUserId>1974546</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Wow, okay then, thanks.</p> <p>The program he made was very confusing to me, and it still is. Honestly that's not how I would structure or even think about coding a program, but I guess it works&#8230;</p> <p>I guess I can try to learn from this program, but overall I think I don't think I'll need this level of complexity in the future.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363528</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363528</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 21:25:50 +0000</pubDate>
								<wikidot:authorUserId>1873534</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363327</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363327</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 16:05:06 +0000</pubDate>
				<wikidot:authorName>lirtosiast</wikidot:authorName>				<wikidot:authorUserId>2005367</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>There are no memory leaks in this program. Memory leaks occur when an If:Then, While, or For block never reaches its End; here the For block reaches an End each time. The first iteration it reaches the End in the subroutine, and the second time it reaches the End in the main program.</p> <p>A subprogram in this form has advantages: storing where to return to in the main program is unnecessary, as the For( has already been pushed onto the stack. Additionally it is possible to have statements like Michael's If:End that conditionally return to the main program. I commend Michael for finding this innovative way to implement subroutines.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363241</guid>
				<title>(no title)</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363241</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 13:13:35 +0000</pubDate>
				<wikidot:authorName>Michael</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Those For loops go to an internal subprogram (the lines in between Lbl M and Lbl Ø).</p> <p>The If sum(not(L1 actually is part of the subprogram, and exits out of the subprogram if L1 is empty.</p> <p>Here's the code:</p> <div class="code"> <pre><code>PROGRAM:G2048 Goto Ø Lbl M If not(sum(L1 End For(A,2,dim(L1 If L1(A:Then For(B,A,2,~1 If L1(B)=L1(B-1) or not(L1(B-1 Then L1(B-1)+L1(B → L1(B-1 0 → L1(B 1 → Ø Else 2 → B End:End:End:End:End Lbl Ø Delvar Ø 0identity(4 → [A] SetupEditor G2048 Menu(&quot;2048&quot;,&quot;LOAD&quot;,L,&quot;NEW&quot;,N Lbl L ~1 → Ø For(A,1,4) For(B,1,4) lG2048(4A+B-4 → [A](A,B End:End Lbl N AxesOff FnOff ZStandard ZInteger Ø+1 → Ø Repeat Ans=45 If Ø:Then Repeat not([A](X,Y randInt(1,4 → X randInt(1,4 → Y End 2+2(rand&gt;.9 → [A](X,Y End ClrDraw For(A,~36,36,18 5A/6 Line(A,~30,A,30 Line(~36,Ans,36,Ans:End For(A,1,4 For(B,1,4 Text(15A-10,18B-5,&quot; &quot; \\15sp [A](A,B If Ans Text(15A-10,18B+1-2int(log(Ans)),Ans End:End Repeat Ans Text(~1,0,90,&quot; &quot; \\1sp getKey End Delvar Ø If Ans=25:Then For(X,1,4 seq([A](B,X),B,1,4 → L1 For(L,~1,0 If L:Goto M End:For(B,1,4 L1(B → [A](B,X End:End:End If Ans=26:Then For(X,1,4 seq([A](X,B),B,4,1,~1 → L1 For(L,~1,0 If L:Goto M End:For(B,1,4 L1(B → [A](X,5-B End:End:End If Ans=34:Then For(X,1,4 seq([A](B,X),B,4,1,~1 → L1 For(L,~1,0 If L:Goto M End:For(B,1,4 L1(B → [A](5-B,X End:End:End If Ans=24:Then For(X,1,4 seq([A](X,B),B,1,4 → L1 For(L,~1,0 If L:Goto M End:For(B,1,4 L1(B → [A](X,B End:End:End:End seq([A](int((A+3)/4),1+4fpart((A-1)/4)),A,1,16) → lG2048</code></pre></div> <p>Also, I didn't know how to put in code blocks. :)</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363086</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363086</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 04:41:22 +0000</pubDate>
				<wikidot:authorName>Michael2_3B</wikidot:authorName>				<wikidot:authorUserId>1974546</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>First of all, please surround your code in [[ code ]] and [[ /code ]] (without the spaces). It looks better.</p> <p>Secondly, you could definitely use this as a learning experience. I would recommend trying to use labels and gotos less, as it's very easy to go wrong with those and have memory leaks. For example,</p> <div class="code"> <pre><code>For(L,~1,0 If L:Goto M End</code></pre></div> <br /> This will cause a memory leak. I don't even understand why you have a for loop there in the first place though, since Goto M will do the exact same thing. Even then you will still have a memory leak though. You just need to make sure that where the goto is jumping to doesn't jump into or out of a code block that's not in the same code block as the goto. When I say code block, I'm sure you know that I mean things like If&#8230;Then&#8230;End or loops such as Repeat or While or For(. <p>Third:</p> <div class="code"> <pre><code>If not(sum(L1 End</code></pre></div> <br /> There is no point to this. I could be wrong, but I believe that End is useless. If you're wanting to stop the program, try the command &quot;Stop&quot; instead. Along with it should be a ClrHome and other cleanup things that you need. <p>There are probably more issues that could be pointed out, but those are the one's I noticed at a quick glance.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2363081</guid>
				<title>Re: G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2363081</link>
				<description></description>
				<pubDate>Mon, 24 Aug 2015 04:32:29 +0000</pubDate>
				<wikidot:authorName>Trenly</wikidot:authorName>				<wikidot:authorUserId>1905506</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>I didn't read it all thoroughly because it wasn't in a code block, but to me it seems like there might be a memory leak or two. I'll go and double check later.</p> <p>Edit: I think all of your lines that read as below cause memory leaks because they are all in &quot;if/then&quot; loops and &quot;for&quot; loops.</p> <div class="code"> <pre><code>If L: Goto M</code></pre></div> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://tibasicdev.wikidot.com/forum/t-1333970#post-2362983</guid>
				<title>G2048</title>
				<link>http://tibasicdev.wikidot.com/forum/t-1333970/g2048#post-2362983</link>
				<description></description>
				<pubDate>Sun, 23 Aug 2015 22:55:51 +0000</pubDate>
				<wikidot:authorName>Michael</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I've created a version of 2048 which I believe is the smallest which is coded in Ti-Basic. Can you make it smaller? If not, enjoy the program as-is!</p> <p>PROGRAM:G2048<br /> Goto Ø<br /> Lbl M<br /> If not(sum(L1<br /> End<br /> For(A,2,dim(L1<br /> If L1(A:Then<br /> For(B,A,2,~1<br /> If L1(B)=L1(B-1) or not(L1(B-1<br /> Then<br /> L1(B-1)+L1(B → L1(B-1<br /> 0 → L1(B<br /> 1 → Ø<br /> Else<br /> 2 → B<br /> End:End:End:End:End<br /> Lbl Ø<br /> Delvar Ø 0identity(4 → [A]<br /> SetupEditor G2048<br /> Menu(&quot;2048&quot;,&quot;LOAD&quot;,L,&quot;NEW&quot;,N<br /> Lbl L<br /> ~1 → Ø<br /> For(A,1,4)<br /> For(B,1,4)<br /> lG2048(4A+B-4 → [A](A,B<br /> End:End<br /> Lbl N<br /> AxesOff<br /> FnOff<br /> ZStandard<br /> ZInteger<br /> Ø+1 → Ø<br /> Repeat Ans=45<br /> If Ø:Then<br /> Repeat not([A](X,Y<br /> randInt(1,4 → X<br /> randInt(1,4 → Y<br /> End<br /> 2+2(rand&gt;.9 → [A](X,Y<br /> End<br /> ClrDraw<br /> For(A,~36,36,18<br /> 5A/6<br /> Line(A,~30,A,30<br /> Line(~36,Ans,36,Ans:End<br /> For(A,1,4<br /> For(B,1,4<br /> Text(15A-10,18B-5,&quot; &quot; \\15sp<br /> [A](A,B<br /> If Ans<br /> Text(15A-10,18B+1-2int(log(Ans)),Ans<br /> End:End<br /> Repeat Ans<br /> Text(~1,0,90,&quot; &quot; \\1sp<br /> getKey<br /> End<br /> Delvar Ø<br /> If Ans=25:Then<br /> For(X,1,4<br /> seq([A](B,X),B,1,4 → L1<br /> For(L,~1,0<br /> If L:Goto M<br /> End:For(B,1,4<br /> L1(B → [A](B,X<br /> End:End:End<br /> If Ans=26:Then<br /> For(X,1,4<br /> seq([A](X,B),B,4,1,~1 → L1<br /> For(L,~1,0<br /> If L:Goto M<br /> End:For(B,1,4<br /> L1(B → [A](X,5-B<br /> End:End:End<br /> If Ans=34:Then<br /> For(X,1,4<br /> seq([A](B,X),B,4,1,~1 → L1<br /> For(L,~1,0<br /> If L:Goto M<br /> End:For(B,1,4<br /> L1(B → [A](5-B,X<br /> End:End:End<br /> If Ans=24:Then<br /> For(X,1,4<br /> seq([A](X,B),B,1,4 → L1<br /> For(L,~1,0<br /> If L:Goto M<br /> End:For(B,1,4<br /> L1(B → [A](X,B<br /> End:End:End:End<br /> seq([A](int((A+3)/4),1+4fpart((A-1)/4)),A,1,16) → lG2048</p> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>