Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Some people say curiosity killed the cat. Other say it was the box and poison. The RSPCA say Erwin Schrödinger needs to lose the right to own pets.

With animal rights activists outside his house. The cat murderer scientist Schrödinger has finally come up with his greatest invention. A special, radioactive mix of unobtanium and handwavium that can have any half life, and a single gram of the product is capable of killing any living creature. Unfortunately, when he tried to test it on his final cat: Bob, he forgot that cats have 9 lives, and so would need 9 grams to kill. With some water but no food, poor Bob will live exactly 1 week (7 days) if the poduct doesn't kill him first.

The task: Given an input of a mass in milligrams and a half-life in milliseconds - both integers which can exceed 2^31, write a program that outputs whether or not the mystery super product kills the cat, or if it's 1 week time limit expires first. Assume true/yes/1/anything specified in the answer is for when he does not die from starvation.

For the product to kill him, a total of 9 grams must decay. So out of a sample of 18 grams, 1 half-life must pass. If the sample contains less than or equal to 9 grams, this will never be achieved, and so it can be immediately assumed 1 week will pass before 9 grams decays.

You may assume: - Bob dies the microsecond 9 grams has decayed. - The change is mass due to decay does not matter. - All days and times follow generally accepted earth time. - The box Bob is sealed in is unbreakable and unopenable, so there is no chance of death from other causes. - Oxygen is also no issue. - If both happen at the exact same time either output is acceptable. - All inputs should be below 2^63-1

Test cases:

Example:

18000 604800001

For 9 grams to decay, exactly 1 half life must pass (18000/2 = 9000 milligrams or 9 grams). 1 half life is 604800001 milliseconds, or 168 hours and 1 millisecond, or exactly 1 week and 1 millisecond. Since Bob dies of hunger at exactly 1 week, the output is false as he died from hunger just before the 9 gram product limit was reached

8000 40000 false

70000 800 true

18000 604800000 either

18000 604800001 false

18000 604799999 true

1 1 false

100000 1 true

1000000000 1000000000 true

Scoring: Naturally we want Bob's suffering to end quickly, and so a shorter half-life is best. Half-life and byte both end in E, so clearly the shortest byte count wins.

share|improve this question
4  
You do realize that a radioactive substance never fully decays – Rohan Jhunjhunwala 14 hours ago
1  
I have no idea what we have to do here... Can you go through each step to find out if the output should be true or false? – βετѧ Λєҫαγ 14 hours ago
    
(must be more than 32-bit integer limit) - both integers was changed to both integers which are strictly less than 2^31,, let me know if this isn't what you wanted. – Easterly Irk 14 hours ago
2  
Could you give us an equation for which outputs give True? – xnor 10 hours ago
1  
@xnor For half-life λ and mass m, m-m*(1/2)**(604800000/λ) > 9000 (or , since the edge case can go either way). – Mego 2 hours ago

Python 3, 33 bytes

lambda a,b:a-a*.5**(6048e5/b)>9e3

Explanation:

         6048e5         # number of milliseconds in 1 week
               /b       # half-lifes per week
  a*.5**(        )      # mgs of substance remaining after 1 week
a-                      # mgs of substance decayed after one week
                  >9e3  # return true if more than 9000mgs has decayed in 1 week

Try here

share|improve this answer

Fourier, 51 bytes

I have to admit that I don't fully understand this program... Mainly just a translation of TheNumberOne's Python code.

oI~M~NI~H604800000~G>H{1}{G/H^(M/2~Mi^~i)N-M>9000@o}

Try it online!

share|improve this answer

CJam (22 bytes)

q~dX.5@6048e5\/#-*9e3>

Online demo

Dissection

A quick explanation of the maths: if the halflife is λ then after time t the proportion of the radioactive material remaining is (1/2)^(t/λ), so the proportion decayed is 1 - (1/2)^(t/λ).

q~d         e# Parse input, ensuring that the later division will use doubles
X.5@6048e5\ e# Rearrange stack to: m 1 0.5 6048e5 λ
/#-*        e# Div, pow, sub, mul, giving the total mass decayed after a week
9e3>        e# Is it OVER 9000! ?
share|improve this answer

Actually, 20 bytes

5╤:6048*/1½ⁿ1-*93╤*<

Try it online!

Explanation:

5╤:6048*/1½ⁿ1-*93╤*<
5╤                    10**5
  :6048               6048
       *              6048*10**5 (milliseconds in 1 week)
        /             divide by half-life
         1½ⁿ          (1/2)**(^)
            1-        1-(^) (% of sample decayed after 1 week)
              *       multiply by sample mass (mass decayed after 1 week)
               93╤*   9*10**3 (9000)
                   <  is 9000 < sample mass decayed?
share|improve this answer

Dyalog APL, 19 bytes

9E3≤⊣-⊣×.5*6048E5÷⊢

9E3≤ is 9000 smaller than or equal to

⊣- the left argument (mass) minus

⊣× the left argument times

.5*  ½ to the power of

6048E5÷⊢ 604800000 divided by the right argument (half-life)

No need for parentheses, as APL is strictly right-to-left.

TryAPL online!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.