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.
First, imagine a number in JavaScript. (Bit of a nail biter here, huh?)
let i = 5
Then, we will construct an incrementor. This is really simple: here is the method.
Make a bracket-string-centric version of eval().
[]["filter"]["constructor"]("return i+1")()
Reconstruct stringy eval() by using +[] as 0, +!+[] as 1, and implicit conversions as ways to create strings. For example, 'false' is (![]+[]), so 'f' is (![]+[])[+[]].
[][
(![] + [])[+[]] + // f
([![]] + [][[]])[+!+[] + [+[]]] + // i
(![] + [])[!+[] + !+[]] + // l
(!![] + [])[+[]] + // t
(!![] + [])[!+[] + !+[] + !+[]] + // e
(!![] + [])[+!+[]] // r
][
([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
([][[]]+[])[+!+[]]+ // n
(![]+[])[!+[]+!+[]+!+[]]+ // s
(!![]+[])[+[]]+ // t
(!![]+[])[+!+[]]+ // r
([][[]]+[])[+[]]+ // u
([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
(!![]+[])[+[]]+ // t
(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
(!![]+[])[+!+[]] // r
]("return i+1")()
Draw the rest of the fucking owl. Final code:
let i = 5; // haha yay
[][
(![] + [])[+[]] + // f
([![]] + [][[]])[+!+[] + [+[]]] + // i
(![] + [])[!+[] + !+[]] + // l
(!![] + [])[+[]] + // t
(!![] + [])[!+[] + !+[] + !+[]] + // e
(!![] + [])[+!+[]] // r
][
([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
([][[]]+[])[+!+[]]+ // n
(![]+[])[!+[]+!+[]+!+[]]+ // s
(!![]+[])[+[]]+ // t
(!![]+[])[+!+[]]+ // r
([][[]]+[])[+[]]+ // u
([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
(!![]+[])[+[]]+ // t
(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
(!![]+[])[+!+[]] // r
](
(!![]+[])[+!+[]]+ // r
(!![]+[])[!+[]+!+[]+!+[]]+ // e
(!![]+[])[+[]]+ // t
([][[]]+[])[+[]]+ // u
(!![]+[])[+!+[]]+ // r
([][[]]+[])[+!+[]]+ // n
(+[![]]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+ // ' '
([![]]+[][[]])[+!+[]+[+[]]]+ // i
(+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]]+ // +
+!+[] // 1
)()
// no virus i swear. execute arbitrary code in your browser console.
Anyway, that's just everyday JS work. It's like step 5 after resizing the button, but a bit before centering the div.
int i = 5;
i ^= printf("The initial value of i is %d\n", i)^
printf("i=i+1; // this increments i\n")^
printf("Trigger very obscure FPU bug %c",(int)((float)8.5953287712*(double)8.5953287713-'?'))/10;
printf("i has now been incremented by 1 : %d\n", i);
Output:
The initial value of i is 5
i=i+1; // this increments i
Trigger very obscure FPU bug
i has now been incremented by 1 : 6
I didn't test other values but they're probably OK.
Trying to avoid using any arithmetic operators, and sticking just to binary (extending beyond 16 bit unsigned ints is left as an exercise for the interested reader):
The time it takes for the counter to increment due to cosmic rays or background radiation is approximately constant, therefore same order as adding one. Same time complexity.
I decided to use NAND instead of NOR, but it's effectively the same thing.
Scala:
//main
@main
def main(): Unit =
var i = 15 //Choose any number here
i = add(i, 1) //this increments i
println(i)
//Adds 2 numbers in the most intuitive way
def add(a: Int, b: Int): Int =
val pairs = split(a).zip(split(b))
val sumCarry = pairs.scanLeft(false, false)((last, current) => fullAdder(current._1, current._2, last._2))
return join(sumCarry.map(_._1).tail.reverse)
//Converts an integer to a list of booleans
def join(list: Seq[Boolean]): Int = BigInt(list.map(if (_) '1' else '0').mkString, 2).toInt
//Converts a list of booleans to an integer
def split(num: Int): Seq[Boolean] = num.toBinaryString.reverse.padTo(32, '0').map(_ == '1')
//Adds 2 booleans and a carry in, returns a sum and carry out
def fullAdder (a: Boolean, b: Boolean, c: Boolean): (Boolean, Boolean) =
(NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), NAND(a, b)))
//The basis for all operations
def NAND(a: Boolean, b: Boolean): Boolean = !a || !b
EDIT: replaced Integer.parseInt with BigInt(...).toInt to fix NumberFormatException with negative numbers.
C# .NET using reflection, integer underflow, and a touch of LINQ. Should work for all integer types. (edit: also works with char values)
// this increments i
private static T Increment<T>(T i)
{
var valType = typeof(T);
var maxField = valType.GetField("MaxValue");
var minField = valType.GetField("MinValue");
if (maxField != null)
{
T maxValue = (T)maxField.GetValue(i);
T minValue = (T)minField.GetValue(i);
var methods = valType.GetTypeInfo().DeclaredMethods;
var subMethod = methods.Where(m => m.Name.EndsWith("op_Subtraction")).First();
T interim = (T)subMethod.Invoke(
null,
[i, maxValue]);
return (T)subMethod.Invoke(
null,
[interim, minValue]);
}
throw new ArgumentException("Not incrementable.");
}
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.
// C++20
#include <concepts>
#include <cstdint>
template <typename T>
concept C = requires (T t) { { b(t) } -> std::same_as<int>; };
char b(bool v) { return char(uintmax_t(v) % 5); }
#define Int jnt=i
auto b(char v) { return 'int'; }
// this increments i:
void inc(int& i) {
auto Int == 1;
using c = decltype(b(jnt));
// edited mistake here: c is a type, not a value
// i += decltype(jnt)(C<decltype(b(c))>);
i += decltype(jnt)(C<decltype(b(c(1)))>);
}
I'm not quite sure it compiles, I wrote this on my phone and with the sheer amount of landmines here making a mistake is almost inevitable.
Just tested this: the "original+" code compiles, but does not increment i.
There were two problems:
b(bool) and b(char) are ambiguous (quick fix: change the signatures to char b(bool&) and auto b(char&& v));
The concept def. has to come after the b functions, even if the constraint is only checked after both, I was unaware of this (fix: define C immediately before void inc(int&)).
but if i gets randomly bitflipped, wouldn't i != i+1 still be false? It would have to get flipped at exactly the right time, assuming that the cpu requests it from memory twice to run that line? It'd probably be cached anyway.
I was thinking you'd need to store the original values, like x=i and y=i+1 and while x != y etc.. but then what if x or y get bitflipped? Maybe we hash them and keep checking if the hash is correct. But then the hash itself could get bitflipped...
Thinking too many layers of redundancy deep makes my head hurt. I'm sure there's some interesting data integrity computer science in there somewhere..
I didn't really dig too deep into it. It might be interesting to see what it actually compiles to.
From what I can remember result of i+1 would have to be stored before it can be compared thus it would be possible for i to experience a bit flip after the result of i+1 is stored.
// this increments i:
// Version 2: Now more efficient; only loops to 50 and just rounds up. That's 50% less inefficient!
function increment(val:number): number {
for (let i:number = 0; i < 50; i = i +1) {
val = val + 0.01
}
return Math.round(val)
}
Funny enough, it is one of the understood operations that I did not integrate on the truth table on-chip. I had some ideas on extra syntax, but the point is to avoid needing to look at reference docs as much as possible and none of my ideas for this one were intuitive enough this satisfy me.