Skip to content

Commit f237275

Browse files
committed
edited posts
1 parent adf2698 commit f237275

File tree

11 files changed

+113
-121
lines changed

11 files changed

+113
-121
lines changed

_posts/2025-12-08-2s-complement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ categories: cs, binary, bits, negative numbers, derivation, proof, math, 2's com
66
permalink: /2s-complement
77
---
88

9-
## Preamble
9+
## Introduction
1010

1111
If you have ever taken CS in high school or college, you have probably been taught about how to represent negative numbers in binary using 2's complement.
1212

_posts/2025-12-10-2nd-diff.md

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
---
22
layout: post
3-
title: 2nd Difference Formula Proof
3+
title: 2nd Difference Derivation & Proof
44
date: 2025-12-10
5-
categories: math, 2nd difference, second difference, proof, derivation, recursion, closed-form solution
5+
categories: math, constant 2nd difference, constant second difference, proof, derivation, recurrence, recursion, closed-form solution, nth term
66
permalink: /2nd-diff
77
---
88

9-
## 1. Preamble<a name="preamble"></a>
9+
## 1. Introduction<a name="intro"></a>
1010

11-
During junior high school, I was introduced to a formula for finding the n<sup>th</sup> term of a certain sequence with a second difference that went something like:
11+
During a math class in junior high school, I was introduced to a formula for finding the n<sup>th</sup> term of a certain sequence with a constant second difference that went something like:
1212

1313
T<sub>n</sub> = 1/2(n)<sup>2</sup> - 1/2(n) + 1, for a sequence `1, 2, 4, 7 ...` and n >= 1.
1414

15-
At first, the formula seemed quite random (similar to the quadratic formula) and I could not wrap my head around it at the time.
15+
The teacher never really explained it though, so it was just a mysterious formula to me at the time.
1616

17-
I wondered how adding a second difference could make the trivial arithmetic formula so complex, and I was inspired by a textbook I was reading ("Concrete Mathematics" by Donald Knuth), so I tried to derive the formula myself.
18-
I also did not like to use things I did not understand, and there is no better way to understand something than to make it yourself from scratch.
19-
20-
I wrote this blog post to share in hopes that someone may find it useful.
17+
Later in college, I was inspired by a textbook I was reading (Chapter 1 of Concrete Mathematics by Donald Knuth on Recurrence Relations) and remembered the mysterious formula, so I tried to derive it myself to better understand it.
2118

2219
## 2. What is a 2<sup>nd</sup> Difference?
2320

2421
Say there is a sequence `1, 2, 4, 7 ...`.
2522
You may have come across quite a few of these types of sequences on IQ tests and whatnot, and it is easy to find that the fifth term is 11 and so on.
2623

27-
However, it is obvious at a glance that the sequence does not follow the familiar arithmetic or geometric formulas, so we cannot yet find an arbitrary n<sup>th</sup> term of the sequence without knowing the previous terms.
28-
Finding the general formula for this will be the goal of this derivation.
24+
However, it is obvious at a glance that the sequence does not follow the familiar formulas for finding the n<sup>th</sup> term of arithmetic or geometric sequences, so we cannot yet find an arbitrary n<sup>th</sup> term of the sequence without first knowing the previous terms.
25+
Finding the general closed-form solution for this will be the goal of this derivation.
2926

3027
The difference or ratio between every term and its subsequent term is not constant, unlike what is needed for a+(n-1)d or ar<sup>n-1</sup> to work properly.
31-
Instead, the difference (which we will now call the 'first difference') increases by a constant amount as the sequence goes on.
28+
Instead, the difference (which we will call the 'first difference') increases by a constant amount as the sequence goes on.
3229

3330
We can see this clearly if we construct a new sequence from the first differences:
3431

@@ -44,15 +41,15 @@ so the sequence of first differences is:
4441

4542
`1, 2, 3 ...`.
4643

47-
Unlike the original sequence, it is easy to see that this sequence has a constant difference of one, so the n<sup>th</sup> term can be found using the familiar formula (albeit with some notation modifications):
44+
Unlike the original sequence, it is easy to see that this sequence has a constant difference of one, so the n<sup>th</sup> term can be found using the familiar formula (albeit with some modifications to notation):
4845

4946
(d<sub>1</sub>)<sub>n</sub> = a<sub>2</sub> + (n - 1)d<sub>2</sub>, for n >= 1,
5047

51-
where (d<sub>1</sub>)<sub>n</sub> is the n<sup>th</sup> first difference, a<sub>2</sub> is the first term of the sequence of first differences (a<sub>1</sub> would be the first term of the original sequence), and d<sub>2</sub> is the difference for the sequence of first differences (or the second difference).
48+
where (d<sub>1</sub>)<sub>n</sub> is the n<sup>th</sup> first difference, a<sub>2</sub> is the first term of the sequence of first differences (a<sub>1</sub> would be the first term of the original sequence), and d<sub>2</sub> is the constant difference between each term in the sequence of first differences (which we will call the 'second difference').
5249

5350
## 3. Derivation of the Formula
5451

55-
Using the approach we naturally used to find that 11 was the fifth term in the original sequence earlier, we can model the sequence as a recursive problem:
52+
Taking inspiration from how we intuitively found that 11 was the fifth term in the original sequence earlier, we model the sequence as a recurrence problem:
5653

5754
T<sub>1</sub> = a<sub>1</sub>;
5855

@@ -64,13 +61,13 @@ Note that it is (d<sub>1</sub>)<sub>n-1</sub> and not (d<sub>1</sub>)<sub>n</sub
6461

6562
so it is
6663

67-
(d<sub>1</sub>)<sub>n</sub> = T<sub>n+1</sub> - T<sub>n</sub>
64+
(d<sub>1</sub>)<sub>n</sub> = T<sub>n+1</sub> - T<sub>n</sub>,
6865

6966
and not
7067

7168
(d<sub>1</sub>)<sub>n</sub> = T<sub>n</sub> - T<sub>n-1</sub>.
7269

73-
Moving on, we expand---or "unfold"---the recursive formula to try and spot some patterns we could use to find a closed-form solution:
70+
Moving on: we expand---or "unfold"---the recursive formula to try and spot some patterns we could use to find a closed-form solution:
7471

7572
T<sub>n</sub> = T<sub>n-1</sub> + (d<sub>1</sub>)<sub>n-1</sub>
7673

@@ -141,16 +138,32 @@ T<sub>n</sub> = a<sub>1</sub> + (n - 1)a<sub>2</sub> + 1/2(n - 2)(n - 1)d<sub>2<
141138

142139
= 1/2(n)<sup>2</sup> - 1/2(n) + 1.
143140

144-
Which matches what I wrote on the [preamble](/2nd-diff#preamble).
145-
You can try out a few examples to check for any counter-examples, but you will not know for certain if it will always be correct unless you prove it, which will be talked about in the next section.
141+
Which matches what I wrote in the [introduction](/2nd-diff#intro).
142+
You can try out a few examples to check for any counter-examples, but you will not know for certain if it will always be correct unless you prove it, which is what we will do in the next section.
146143

147144
## 4. Proof by Mathematical Induction
148145

149146
Continuing from the previous section: the derivation makes sense, and we can try a few examples, but how do we know for sure that there is no counter-example somewhere we did not check?
150147
We need to be mathematically rigorous to show our formula will always work.
151148

152-
Fortunately, there is a convenient method called "proof by induction" that fits nicely for our sequences situation.
153-
If we show that our formula is true for a base case and also that it is true for any n if it was true for (n - 1), we can confidently conclude that the formula is true for all n.
149+
Fortunately, there is a convenient method called "proof by induction" that fits nicely into our sequences situation.
150+
If we show that our formula is true for a base case and also that it is true for any n if it was true for (n - 1), we can confidently conclude that the formula is true for all positive integer n.
151+
152+
### 4.2 Base Case<a name="base"></a>
153+
154+
Plugging in the formula for T<sub>1</sub>:
155+
156+
T<sub>1</sub> = a<sub>1</sub> + (n - 1)a<sub>2</sub> + 1/2(n - 2)(n - 1)d<sub>2</sub>
157+
158+
= a<sub>1</sub> + (1 - 1)a<sub>2</sub> + 1/2(1 - 2)(1 - 1)d<sub>2</sub>, for n = 1
159+
160+
= a<sub>1</sub> + (0)a<sub>2</sub> + 1/2(-1)(0)d<sub>2</sub>, for n = 1
161+
162+
= a<sub>1</sub> + 0 + 0
163+
164+
= a<sub>1</sub>.
165+
166+
Which matches our definition, hence proving that the formula is true for n = 1.
154167

155168
### 4.1 Induction Step<a name="step"></a>
156169

@@ -195,28 +208,11 @@ T<sub>n</sub> = a<sub>1</sub> + ((n - 1) - 1)a<sub>2</sub> + 1/2((n - 1) - 2)((n
195208
= a<sub>1</sub> + (n - 1)a<sub>2</sub> + 1/2(n - 2)(n - 1)d<sub>2</sub>
196209

197210
That matches our formula, and hence proves that the formula will hold true for any n if (n - 1) is true.
198-
However, we are still missing our base case (n = 1), which is covered in the next subsection.
199-
200-
### 4.2 Base Case
201-
202-
Plugging in the formula for T<sub>1</sub>:
203-
204-
T<sub>1</sub> = a<sub>1</sub> + (n - 1)a<sub>2</sub> + 1/2(n - 2)(n - 1)d<sub>2</sub>
205-
206-
= a<sub>1</sub> + (1 - 1)a<sub>2</sub> + 1/2(1 - 2)(1 - 1)d<sub>2</sub>, for n = 1
207-
208-
= a<sub>1</sub> + (0)a<sub>2</sub> + 1/2(-1)(0)d<sub>2</sub>, for n = 1
209-
210-
= a<sub>1</sub> + 0 + 0
211-
212-
= a<sub>1</sub>.
213211

214-
Which matches our definition, and hence proves that the formula is true for n = 1.
215-
Combining that with the earlier [induction step](/2nd-diff#step), we get that it is also true for n = 2, n - 1 = 1.
216-
Then that also shows that the formula is true for n = 3, n - 1 = 2.
217-
This then continues on infinitely, which shows that our formula holds true for any n.
212+
### 4.3 Conclusion
218213

219-
QED.
214+
Combining the [base case](/2nd-diff#base) with the [induction step](/2nd-diff#step), we get that it is also true for n = 2 (as it is true for n = 1), which also shows that the formula is true for n = 3 (as it is true for n = 2) and so on.
215+
This continues infinitely, showing our formula holds true for any positive integer n. QED.
220216

221217
## 5. Conclusion
222218

_site/404.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
<meta property="og:locale" content="en_US" />
1010
<meta name="description" content="Technical blog" />
1111
<meta property="og:description" content="Technical blog" />
12-
<link rel="canonical" href="https://de-soot.github.io/404.html" />
13-
<meta property="og:url" content="https://de-soot.github.io/404.html" />
12+
<link rel="canonical" href="http://localhost:4000/404.html" />
13+
<meta property="og:url" content="http://localhost:4000/404.html" />
1414
<meta property="og:site_name" content="de_soot" />
1515
<meta property="og:type" content="website" />
1616
<meta name="twitter:card" content="summary" />
1717
<meta property="twitter:title" content="de_soot" />
1818
<script type="application/ld+json">
19-
{"@context":"https://schema.org","@type":"WebPage","description":"Technical blog","headline":"de_soot","url":"https://de-soot.github.io/404.html"}</script>
19+
{"@context":"https://schema.org","@type":"WebPage","description":"Technical blog","headline":"de_soot","url":"http://localhost:4000/404.html"}</script>
2020
<!-- End Jekyll SEO tag -->
21-
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="https://de-soot.github.io/feed.xml" title="de_soot" /></head>
21+
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="de_soot" /></head>
2222
<body><header class="site-header" role="banner">
2323

2424
<div class="wrapper"><a class="site-title" rel="author" href="/">de_soot</a><nav class="site-nav">

_site/about.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
<meta property="og:locale" content="en_US" />
1010
<meta name="description" content="Technical blog" />
1111
<meta property="og:description" content="Technical blog" />
12-
<link rel="canonical" href="https://de-soot.github.io/about" />
13-
<meta property="og:url" content="https://de-soot.github.io/about" />
12+
<link rel="canonical" href="http://localhost:4000/about" />
13+
<meta property="og:url" content="http://localhost:4000/about" />
1414
<meta property="og:site_name" content="de_soot" />
1515
<meta property="og:type" content="website" />
1616
<meta name="twitter:card" content="summary" />
1717
<meta property="twitter:title" content="About" />
1818
<script type="application/ld+json">
19-
{"@context":"https://schema.org","@type":"WebPage","description":"Technical blog","headline":"About","url":"https://de-soot.github.io/about"}</script>
19+
{"@context":"https://schema.org","@type":"WebPage","description":"Technical blog","headline":"About","url":"http://localhost:4000/about"}</script>
2020
<!-- End Jekyll SEO tag -->
21-
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="https://de-soot.github.io/feed.xml" title="de_soot" /></head>
21+
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="de_soot" /></head>
2222
<body><header class="site-header" role="banner">
2323

2424
<div class="wrapper"><a class="site-title" rel="author" href="/">de_soot</a><nav class="site-nav">

_site/dark-mode.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
<meta property="og:locale" content="en_US" />
1010
<meta name="description" content="Alternative title: System default theme for webpages" />
1111
<meta property="og:description" content="Alternative title: System default theme for webpages" />
12-
<link rel="canonical" href="https://de-soot.github.io/dark-mode" />
13-
<meta property="og:url" content="https://de-soot.github.io/dark-mode" />
12+
<link rel="canonical" href="http://localhost:4000/dark-mode" />
13+
<meta property="og:url" content="http://localhost:4000/dark-mode" />
1414
<meta property="og:site_name" content="de_soot" />
1515
<meta property="og:type" content="article" />
1616
<meta property="article:published_time" content="2024-09-07T00:00:00+08:00" />
1717
<meta name="twitter:card" content="summary" />
1818
<meta property="twitter:title" content="Dark Mode without Javascript" />
1919
<script type="application/ld+json">
20-
{"@context":"https://schema.org","@type":"BlogPosting","dateModified":"2024-09-07T00:00:00+08:00","datePublished":"2024-09-07T00:00:00+08:00","description":"Alternative title: System default theme for webpages","headline":"Dark Mode without Javascript","mainEntityOfPage":{"@type":"WebPage","@id":"https://de-soot.github.io/dark-mode"},"url":"https://de-soot.github.io/dark-mode"}</script>
20+
{"@context":"https://schema.org","@type":"BlogPosting","dateModified":"2024-09-07T00:00:00+08:00","datePublished":"2024-09-07T00:00:00+08:00","description":"Alternative title: System default theme for webpages","headline":"Dark Mode without Javascript","mainEntityOfPage":{"@type":"WebPage","@id":"http://localhost:4000/dark-mode"},"url":"http://localhost:4000/dark-mode"}</script>
2121
<!-- End Jekyll SEO tag -->
22-
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="https://de-soot.github.io/feed.xml" title="de_soot" /></head>
22+
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="de_soot" /></head>
2323
<body><header class="site-header" role="banner">
2424

2525
<div class="wrapper"><a class="site-title" rel="author" href="/">de_soot</a><nav class="site-nav">

0 commit comments

Comments
 (0)