1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <math.h>

typedef unsigned int Cents_t;
typedef double Dollars_t;
//typedef float Dollars_t;

#define MEASURED_DAILY_RATIO (7217612.38/5690199.43)
//#define MEASURED_DAILY_RATIO (7217612.38f/5690199.43f)
#define MEASURED_END_PRICE_cents 219817795

#define MINIMUM_INITIAL_PRICE_cents (20*100)
#define MAXIMUM_INITIAL_PRICE_cents (200*100)

Cents_t centsFromFloat_round(Dollars_t f){
  return roundf(f * 100);
}
Cents_t centsFromFloat_floor(Dollars_t f){
  return floor(f * 100);
}
Cents_t centsFromFloat_ceiling(Dollars_t f){
  return floor(f * 100) + 1;
}

// Note that improper combinations of initial price and daily ratio can easily lead to infinite loops here.
void searchExactMatches(Cents_t (*centsFromFloat)(Dollars_t)){
  Cents_t initialPrice_cents;
  for(initialPrice_cents = MINIMUM_INITIAL_PRICE_cents; initialPrice_cents < MAXIMUM_INITIAL_PRICE_cents; initialPrice_cents++){
    // End price if ended on first day.
    unsigned int endPrice_cents = initialPrice_cents;
    unsigned int day = 0;
    // Simulate days until meet or exceed measured end price
    for(day = 0; endPrice_cents < MEASURED_END_PRICE_cents; day++){
      Dollars_t preRounding_endPrice = (endPrice_cents / 100.0) * MEASURED_DAILY_RATIO;
      endPrice_cents = centsFromFloat(preRounding_endPrice);
    }
    // If precisely met target end price, print out results.
    if(endPrice_cents == MEASURED_END_PRICE_cents)
      printf("**MATCH: initial price %u cents, %u days in play\n", initialPrice_cents, day);
    else
      printf("NO MATCH: initial price %u cents, %u days in play, end price %u cents\n", initialPrice_cents, day, endPrice_cents);
  }
}

int main(int argc, char **argv){
  printf("BEGIN SIMULATIONS\n");
  searchExactMatches(&centsFromFloat_round);
  searchExactMatches(&centsFromFloat_floor);
  searchExactMatches(&centsFromFloat_ceiling);
  printf("END SIMULATIONS\n");
}