8
1
Inspired by http://xkcd.com/1331/
In this xkcd comic, there are several gifs that all blink with different frequency. I want to know what the period would be if was all a single GIF. Given a list of numbers representing the individual frequencies, output the period of the overall GIF.
Formal Definition
Input:
N
f1
f2
.
.
.
fN
Output:
P
Where N is the number of frequencies, fi is the ith frequency, and P is the resulting period of the total GIF.
You may choose to use any delimiting character you want(instead of \n), and you may exclude the N if you wish, and it will be inferred by the number of delimeters.
Some specifics
Frequencies are the closest double-precision floating point representation of the numbers provided as input.
The outputted period is a 64-bit unsigned integer, rounded(up on 0.5) to the nearest whole. Any input that would produce a period bigger than 2^64-1 is considered undefined behaviour.
Likewise any input <= 0 is considered undefined behaviour.
Win Condition
Code golf so shortest code wins!
Possible spoiler... wouldn't this just be the product of the given frequencies? – danmcardle – 2014-02-19T23:50:46.473
@crazedgremlin No it isn't, but you are pretty close. – Victor Stafusa – 2014-02-20T00:30:08.950
@crazedgremlin - If A is 2s and B is 4s, then the resulting period would be 4s, not 8s. – Digital Trauma – 2014-02-20T00:36:36.953
Ah, I see, thanks. @Cruncher, what exactly do you mean by "frequency"? Repetitions per second or the amount of time a repetition takes? I assume the former, as that is usually what frequency means. – danmcardle – 2014-02-20T00:41:12.073
There are at least 2 methods for this: Take the overall frequency as the GCD of the input frequencies and invert it. Or take the input frequencies, invert them all to get the periods and take the LCM as the overall period. I took the GCD. @DavidCarraher took the LCM. You just need to cope with the non-integers. – Victor Stafusa – 2014-02-20T02:20:30.117
I'm not going to post this since it's just expressing the already posted method in a more concise language, but the J answer would be
1%+./
with the requirement that the frequencies be given withoutN
and space separated. Alternatively,*./1%
with the same requirements. Right tool for the job. – Gareth – 2014-02-20T09:59:03.793@crazedgremlin The inputs are frequencies (# of repetitions in unit time). And the output is period (units of time for each repetition) – Cruncher – 2014-02-20T13:42:22.357