2014-09-25

Change 1 character puzzle #2

Add or replace at most 1 character in the longest line of the following program so that it will print 20 dots. There are at least 10 solutions.

#include <stdio.h>
int main() {
  int i, n = 20; for (i = -n; n > i; i++) putchar('.');
  return 0;
}

SPOILER ALERT! Here are 10 solutions:

int i, n = 10; for (i = -n; n > i; i++) putchar('.');
int i, n = 20; for (i = -0; n > i; i++) putchar('.');
int i, n = 20; for (i = -!n; n > i; i++) putchar('.');
int i, n = 20; for (i = n-n; n > i; i++) putchar('.');
int i, n = 20; for (i = -n; !n > i; i++) putchar('.');
int i, n = 20; for (i = -n; 0 > i; i++) putchar('.');
int i, n = 20; for (i = -n; n * i; i++) putchar('.');
int i, n = 20; for (i = -n; n & i; i++) putchar('.');
int i, n = 20; for (i = -n; n = i; i++) putchar('.');
int i, n = 20; for (i = -n; n , i; i++) putchar('.');

Change 1 character puzzle #1

Add or replace at most 1 character in the longest line of the following program so that it will print 20 dots. There are at least 3 solutions.

#include <stdio.h>
int main() {
  int i, n = 20; for (i = 0; i < n; i--) putchar('.');
  return 0;
}

SPOILER ALERT! Here are 3 solutions:

int i, n = 20; for (i = 0; -i < n; i--) putchar('.');
int i, n = 20; for (i = 0; i < n; n--) putchar('.');
int i, n = 20; for (i = 0; i + n; i--) putchar('.');

2014-09-08

How to keep to your commitments without using up too much willpower

This blog post explains how hyperbolic discounting causes you to change your mind, and how to trick it so that you can keep to your commitments without using up too much willpower.

A student who doesn't learn as much as he wants to

A student has an exam on Friday. He has 4 days (Monday, Tuesday, Wednesday and Thursday) to learn. The more he learns, the more points he will get for the exam. On each learning day, he may choose to skip learning and go to a party instead. It happens that the previous week he decides that he will learn for 3 days, but during the week it turns out that he learns only once, and goes to parties on 3 days. How can this happen?

Explanation with hyperbolic discounting

This can be explained by assuming that the mind does hyperbolic discounting, i.e. it computes today's utility of a future event by dividing the utility of the event divided by the number of days of delay. See the more specific example below:

Hyperbolic discounting for time for the utility of u today:

  • same thing tomorrow: u/2
  • same thing in 2 days: u/3
  • same thing in 3 days: u/4
  • same thing in 4 days: u/5
  • same thing in 5 days: u/6

Utility function for today:

  • take the exam, get x points (where x is the number of days previously spent learning for the exam, x in 0..4): 60*x
  • party: 29
  • have a rest: 0
  • learn: 0
Previous Sunday:
  • learn on Monday–Friday: 60*4/6 = 40
  • party on Monday–Friday: 29/2+29/3+29/4+29/5 = 37.216...
  • party on Monday–Tuesday, learn on Wednesday and Thursday: 29/2+29/3+60*2/6 = 44.166...
  • learn on Monday–Tuesday, party on Wednesday and Thursday: 60*2/6+29/4+29/5 = 33.05
  • party on Monday, learn on Tuesday–Thursday: 29/2+60*3/6 = 44.5
  • party on Monday–Wednesday, learn on Thursday: 29/2+29/3+29/4+60/6 = 41.416...
  • ... (there are a few other cases)
  • what will happen: The student decides to party on Monday, and to learn on Tuesday--Thursday.

Monday:

  • learn on Monday–Thursday: 60*4/5 = 48
  • party on Monday, learn on Tuesday–Thursday: 29+60*3/5 = 65
  • party on Monday–Tuesday, learn on Wednesday–Thursday: 29+29/2+60*2/5 = 67.5
  • party on Monday–Wednesday, learn on Thursday: 29+29/2+29/3+60/5 = 65.166...
  • party on Monday–Thursday: 29+29/3+29/3+29/4 = 55.583...
  • ... (there are a few other cases)
  • what will happen: The student parties on Monday, and decides to party on Tuesday, and to learn on Wednesday--Thursday.

Tuesday:

  • learn on Tuesday–Thursday: 60*3/4 = 45
  • party on Tuesday, learn on Wednesday–Thursday: 29+60*2/4 = 59
  • party on Tuesday–Wednesday, learn on Thursday: 29+29/2+60/4 = 58.5
  • party on Tuesday–Thursday: 29+29/3+29/3 = 48.333...
  • ... (there are a few other cases)
  • what will happen: The student parties on Tuesday, and decides to learn on Wednesday--Thursday.

Wednesday:

  • learn on Wednesday–Thursday: 60*2/3 = 40
  • party on Wednesday, learn on Thursday: 29+60/3 = 49
  • party Wednesday–Thursday: 29+29/2 = 43.5
  • ... (there is one more case)
  • what will happen: The student parties on Wednesday, and decides to learn on Thursday.

Thursday:

  • learn on Thursday: 60/2 = 30
  • party on Thursday: 29
  • what will happen: The student learns on Thursday.

Friday:

  • take the exam on Friday: 60
  • don't take the exam on Friday: 0
  • party on Friday: 29
  • what will happen: The student takes the exam, scoring 1 point.

So hyperbolic discounting causes the student to change his mind several times during the week: he will do something else on Tuesday and Wednesday than what he decides on Sunday. On Sunday he was planning to learn later next week, and get 3 point on the exam. But he will end up learning only on one day, and getting only 1 point on the exam.

How to keep to your commitments you've made to yourself?

Instead of hyperbolic discounting, let's calculate with exponential discounting.

Exponential discounting for time for the utility of u today:

  • same thing tomorrow: u/2
  • same thing in 2 days: u/4
  • same thing in 3 days: u/8
  • same thing in 4 days: u/16
  • same thing in 5 days: u/32

It can be proven that with exponential discounting you would never change your mind: the relative utility of outcomes remains the same as time passes, there won't be any inconsistencies.

Unfortunately, some experiments show that hyperbolic discounting is hardwired into your mind, and you have no conscious power over it, you can't replace it with exponential discounting at will.

Excercising willpower is easier said than done, particularly that each person has a limited amount of willpower, and if it gets depleted, one has to wait for it to get refilled. So depending on the challenges you had previously on that day, you may have no willpower left to stay away from the party.

Another option you have is limiting and committing yourself to the same choice for a prolonged period of time. For example, if the student commits himself to do the same activity on Monday, Tuesday and Wednesday, then he will choose learning on all the 4 days, and he will actually do it.