this post was submitted on 06 Jan 2025
158 points (95.9% liked)

Programmer Humor

19923 readers
1918 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

A couple of years ago, my friend wanted to learn programming, so I was giving her a hand with resources and reviewing her code. She got to the part on adding code comments, and wrote the now-infamous line,

i = i + 1 #this increments i

We've all written superflouous comments, especially as beginners. And it's not even really funny, but for whatever reason, somehow we both remember this specific line years later and laugh at it together.

Years later (this week), to poke fun, I started writing sillier and sillier ways to increment i:

Beginner level:

# this increments i:
x = i 
x = x + int(True)
i = x

Beginner++ level:

# this increments i:
def increment(val):
   for i in range(val+1):
      output = i + 1
   return output

Intermediate level:

# this increments i:
class NumIncrementor:
	def __init__(self, initial_num):
		self.internal_num = initial_num

	def increment_number(self):
		incremented_number = 0
		# we add 1 each iteration for indexing reasons
		for i in list(range(self.internal_num)) + [len(range(self.internal_num))]: 
			incremented_number = i + 1 # fix obo error by incrementing i. I won't use recursion, I won't use recursion, I won't use recursion

		self.internal_num = incremented_number

	def get_incremented_number(self):
		return self.internal_num

i = input("Enter a number:")

incrementor = NumIncrementor(i)
incrementor.increment_number()
i = incrementor.get_incremented_number()

print(i)

Since I'm obviously very bored, I thought I'd hear your take on the "best" way to increment an int in your language of choice - I don't think my code is quite expert-level enough. Consider it a sort of advent of code challenge? Any code which does not contain the comment "this increments i:" will produce a compile error and fail to run.

No AI code pls. That's no fun.

you are viewing a single comment's thread
view the rest of the comments
[–] kogasa@programming.dev 15 points 3 days ago* (last edited 3 days ago) (2 children)

Let f(x) = 1/((x-1)^(2)). Given an integer n, compute the nth derivative of f as f^((n))(x) = (-1)^(n)(n+1)!/((x-1)^(n+2)), which lets us write f as the Taylor series about x=0 whose nth coefficient is f^((n))(0)/n! = (-1)^(-2)(n+1)!/n! = n+1. We now compute the nth coefficient with a simple recursion. To show this process works, we make an inductive argument: the 0th coefficient is f(0) = 1, and the nth coefficient is (f(x) - (1 + 2x + 3x^(2) + ... + nx^(n-1)))/x^(n) evaluated at x=0. Note that each coefficient appearing in the previous expression is an integer between 0 and n, so by inductive hypothesis we can represent it by incrementing 0 repeatedly. Unfortunately, the expression we've written isn't well-defined at x=0 since we can't divide by 0, but as we'd expect, the limit as x->0 is defined and equal to n+1 (exercise: prove this). To compute the limit, we can evaluate at a sufficiently small value of x and argue by monotonicity or squeezing that n+1 is the nearest integer. (exercise: determine an upper bound for |x| that makes this argument work and fill in the details). Finally, evaluate our expression at the appropriate value of x for each k from 1 to n, using each result to compute the next, until we are able to write each coefficient. Evaluate one more time and conclude by rounding to the value of n+1. This increments n.

[–] SatouKazuma@programming.dev 2 points 1 day ago (1 children)

OP asked for code, not a lecture in number theory.

That said, as someone with a degree in math...I gotta respect this.

[–] kogasa@programming.dev 2 points 1 day ago* (last edited 1 day ago)

The argument describes an algorithm that can be translated into code.

1/(1-x)^(2) at 0 is 1

(1/(1-x)^(2) - 1)/x = (1 - 1 + 2x - x^(2))/x = 2 - x at 0 is 2

(1/(1-x)^(2) - 1 - 2x)/x^(2) = ((1 - 1 + 2x - x^(2) - 2x + 4x^(2) - 2x^(3))/x^(2) = 3 - 2x at 0 is 3

and so on

[–] lime@feddit.nu 7 points 2 days ago

calm down, mr Knuth