-4

UPDATE: Apparently, people think this is a homework assignment and are down voting my question. It's not. It's a re-life business logic question which is pretty complicated math as you can see from the answer. I would appreciate a little positive votes so I can continue to use this site.

I need to figure out a formula for a free nights calculator. The free nights options can vary. For example pay for 6, stay for 7. Pay for 5, stay for 7.

Given the two numbers in the variable, I need to be able to pass the number of nights, and calculate the number you pay for and the number you get free.

For example:

Pay for 5, stay for 7.

1 night = pay for 1, 0 free
2 nights = pay for 2, 0 free
3 nights = pay for 3, 0 free
4 nights = pay for 4, 0 free
5 nights = pay for 5, 0 free
6 nights = pay for 5, 1 free
7 nights = pay for 5, 2 free
8 nights = pay for 6, 2 free
9 nights = pay for 7, 2 free
10 nights = pay for 8, 2 free
11 nights = pay for 9, 2 free
12 nights = pay for 10, 2 free
13 nights = pay for 10, 3 free
14 nights = pay for 10, 4 free
15 nights = pay for 11, 4 free
16 nights = pay for 12, 4 free
17 nights = pay for 13, 4 free
18 nights = pay for 14, 4 free

I was thinking I needed to use modulus, but could not figure it out.

$pay_for = 5;
$stay_for = 7;
for($i=1;$i<=18;$i++) {
    $modulo = $i % $pay_for;
    echo("nights:{$i}, remainder:{$modulo}<br>\n");
}

Thanks for any help!

EDITED: Left out a line in my examples above so updated.

Thom
  • 263
  • 2
  • 13
  • 2
    What language? I'm assuming PHP? – Carcigenicate Jan 16 '17 at 22:32
  • Also, but more specific about what you're having problems with. As it stands, it seems like you want us to do your homework. – Carcigenicate Jan 16 '17 at 22:33
  • I am writing in PHP. Sorry, thought a for loop was generic enough to not really need to specify the language. – Thom Jan 16 '17 at 22:41
  • I haven't had homework in 40 years, but thanks for the compliment. Not sure why, but having problem trying to wrap my head about this math. I can divide the nights by the "pay for" and that get me to 7 nights if I floor the division. But at 8 nights, I can't figure out how to get the "pay for 6". – Thom Jan 16 '17 at 22:43
  • 1
    It usually a good idea to specify the language since anyone answering may want to use the same language. If you don't specify, they'll need to guess, or use pseudocode, and either may glaze over a language specific detail that may cause you further problems. And I mentioned homework since this looks *exactly* like a question from a student wanting us to do their homework. That's probably what the downvotes are for, as unfair as that is. For future reference, this kind of question isn't well suited for the site, since you're more asking for general guidance, instead of a easily Googleable ques – Carcigenicate Jan 16 '17 at 22:49
  • And I must be tired. I can't see a relationship between the nights stayed and the number of free nights recieved. This looks like it should just be a linear equation (with some rounding). – Carcigenicate Jan 16 '17 at 22:53
  • Sorry, tried to google it and found nothing, except a lot of pretty complicated math questions on this site. So, thought this fit. Now that you know the language does that lend itself to an answer? Since you have commented on it, my experience has show that others will stay away from it, so I was hoping for a little direction. It almost seems as if I need to calculate he number of free nights and then just deduct that from the total nights, but that is not a retainer issue. – Thom Jan 16 '17 at 22:56
  • I'm looking it over. I just got off work after 4 hours of sleep, so I'm not really in my prime. Again though, it doesn't appear to be a consistent relationship. A free night is added at 6 and 7 nights, but not again until 13 nights. If it's not anything that can be reduced to a simple math equation, you may need to create some kind of ruleset that describes it. – Carcigenicate Jan 16 '17 at 23:00
  • The data is also missing days in the left column. Is that intentional? – Carcigenicate Jan 16 '17 at 23:05
  • Sorry, what's missing? – Thom Jan 16 '17 at 23:10
  • "14 nights =" is missing. The pattern seems to be every 6 days, + 1 day for every 2 free nights accumulated, the next 2 days give a free night. I can't verify that without that row though. – Carcigenicate Jan 16 '17 at 23:18
  • Added row for 14 nights above. This is not an easy equation. Very frustrating. – Thom Jan 16 '17 at 23:28

1 Answers1

1

This was actually much simpler than I thought it was going to be. You were right that modulus was the key. I'm going to write this in pseudocode since I haven't written PHP in like a year, and you're right, this is language agnostic.

function earned-free-night (nights-stayed) {
    return 5 <= (nights-stayed % 7) <= 6;
}

function display-sheet () {
    for (var night = 1, free = 0; night <= 18; night++) {
        print("Night#: " + night + ", Free nights: " + free);

        if (earned-free-night(night)) {
            free++;
        }   
    }
}

If the number of nights stayed, modulo 7, is a 5 or a 6, add a free night.

Here's some working (Clojure) code that I used to test it. Compilers are readily available online if you want to try it:

(defn free-night? [nights-stayed]
  (<= 5 (rem nights-stayed 7) 6))

(defn -main []
  (loop [night-n 1
         free-nights 0]
    (println (str "Night#: " night-n ", Free nights: " free-nights))
    (if (<= night-n 18)
      (recur (inc night-n)
             (if (free-night? night-n) (inc free-nights) free-nights)))))
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • OH MY! This is awesome. I have tried to do this in PHP and swapped some of your hard numbers for my variables. Here is what I have. I am not sure my assumption that the modulus needs to be => the pay for nights is correct, but seems to work. I will do a bunch of testing. Sorry, not sure how to put code in my reply. – Thom Jan 17 '17 at 01:19
  • $pay_for = 6; $stay_for = 7; echo("pay for {$pay_for}, stay for {$stay_for}
    \n
    \n"); for($night = 1,$free = 0;$night<=22;$night++) { $modu = $night % $stay_for ; $not_free = $night - $free; echo("nights:{$night}, pay:{$not_free} free:{$free}
    \n"); if($modu >= $pay_for) { $free++; } } die("DONE");
    – Thom Jan 17 '17 at 01:21
  • 1
    @Thom It's hard to read code in comments, especially PHP, but that seems correct. – Carcigenicate Jan 17 '17 at 11:58