# Number of cigarettes that can be made from a given number of butts

## Assumption

A cigarette can be made by combining four cigarette butts. Cigarette butts last infinitely until smoked.

## Explanation

Say you have 31 butts. That means, you can make 7 cigarettes from 28 butts and have 3 left over.
Now, these 7 cigarettes will yield 7 butts when you're done smoking them. That means you have 3 + 7 = 10 butts.
These 10 butts make 2 new cigarettes and 2 butts are left over, but the two new cigarettes yield 2 more butts, for a total of 4 butts remaining.
Therefore, 31 butts means you can smoke 10 cigarettes in total.

## The question

Given a number of butts N, where 0 <= N < 1000000, find out how many cigarettes can be made from those N butts.

### Input format

A single line containing the integer N.

### Output format

Print the number of cigarettes that can be made from N butts.

## Test cases

1. Input:
31

Output:
10

2. Input:
7

Output:
2

3. Input:
4

Output:
1


For N <= 3, the output should be 0.

My personal best C solution is 66 bytes. A friend helped reduce it to 63 bytes, and someone else I know was able to come up with a 43-byte solution (also in C).

• I downvoted this for being a chameleon challenge because it reduces to a trick that makes it trivial.
– xnor
Feb 4 at 23:03
• The trick is that the "recursive" look at the problem is a distraction. With 31 butts, you can turn four of them into a cigarette, smoke it, and gain a butt to reach 28. So we are simply counting the number of times we can subtract 3 before we reach n<4.
– Lynn
Feb 5 at 3:33

# JavaScript (ES6), 10 bytes

Unless I'm missing something, this boils down to:

n=>~-n/3|0


Try it online!

Or 9 bytes with BigInts.

# C (gcc), 14 bytes

f(n){n=~-n/3;}


Try it online!

• This was the eventual best 43 byte solution a friend ended up on, too -- main(c){scanf("%d",&c);printf("%d",~-c/3);}. I can't figure out how to shorten the scanf/printf though, unfortunately. Feb 5 at 8:21

# Charcoal, 6 bytes

Ｉ÷↔⊖Ｎ³


Try it online! Link is to verbose version of code. Explanation:

    Ｎ   Input as a number
⊖    Decremented
↔     Absolute value
÷      Integer divided by
³  Literal integer 3
Ｉ       Cast to string
Implicitly print


# Jelly, 4 bytes

s3ṖL


A monadic Link that accepts the number of butts and yields the number of cigarettes.

Try it online! Or see the test-suite.

##### How?

Group the butts into threes, and consider the last up to three butts your "Pile". Take each of the other groups of three butts in turn and combine with one of your Pile, replenishing your Pile with the generated butt.

s3ṖL - Link: integer, Butts
s3   - split into threes (rightmost lacking if necessary)
Ṗ  - pop (remove your Pile from the right side)
L - length

• @emanresuA doesn't work when n=0 (should give 0) Feb 4 at 23:37

# Funge-98, 12 bytes

&:1-3/\!!*.@


Try it online!

# 05AB1E, 3 bytes

<3÷


Explanation:

<    # Decrease the (implicit) input-integer by 1
3÷  # Then integer-divide it by 3
# (after which the result is output implicitly)


Note: Integer-division in 05AB1E (built in Elixir) will round towards 0 for negative values, so this works even for $$\n=0\$$.

• code means heart divides
– qwr
Feb 7 at 4:31

# Retina 0.8.2, 11 bytes

.+
$* ...\B  Try it online! Link is to verbose version of code. Explanation: .+$*


Convert to unary.

...\B


For every three butts, if there is at least one other butt left over, you can smoke another cigarette, which will leave a butt, which therefore replenishes the butt left over. So this just counts the number of groups of three butts while still leaving at least one butt left over.

# Rattle, 6 bytes

|-/3R1


Try it Online!

# Explanation

|         take input
-        decrement
/3      divide by 3
R1    reformat as int, implicit output


Alternatively, the code could be |!-/3i