## Problem statement

Event EA and EB needs to be done every A sec and B sec. Always make sure EA occurs before EB if A and B are multiples of each other.

## More explanation

Say EA need to occur every 2 sec and EB needs be occur every 5 sec.
EA would occur at
2, 4, 6, 8, 10, 12, 14, 16, 18...
EB would occur at
5, 10, 15, 20, 25, 30, 35, 40,...

Hence combining both together
2 - EA occurs
4 - EA occurs
5 - EB occurs
6 - EA occurs
8 - EA occurs
10 - EA and EB occurs
12 - EA occurs
14 - EA occurs
15 - EB occurs
and so on

Hence we need to count things in irregular interval (2, 2, 1, 1, 2, 2, 2, 2, 1, 1 ...)

## Solution

#include <stdio.h>

#define MAX_LIMIT 25

#define min(i,j) (((i) < (j)) ? (i) : (j))
int main()
{
int a = 2, b = 5;
int ta = a, tb = b;
int t = 0, x;

for(t = 0; t < MAX_LIMIT; t = t + x) {
x = min(ta - t, tb - t);
x = (x == 0) ? min(a, b) : x;
if((ta - t) < (tb - t)) {
ta = ta + a;
printf("do EA ");
}else if((ta - t) > (tb - t)){
tb = tb + b;
printf("do EB ");
}else {
ta = ta + a;
tb = tb + b;
printf("do EA and EB ");
}
printf("t is %d, interval is %d\n", t + x, x);
}
return 0;
}


Output of above program is

do EA t is 2, interval is 2
do EA t is 4, interval is 2
do EB t is 5, interval is 1
do EA t is 6, interval is 1
do EA t is 8, interval is 2
do EA and EB t is 10, interval is 2
do EA t is 12, interval is 2
do EA t is 14, interval is 2
do EB t is 15, interval is 1
do EA t is 16, interval is 1
do EA t is 18, interval is 2
do EA and EB t is 20, interval is 2
do EA t is 22, interval is 2
do EA t is 24, interval is 2
do EB t is 25, interval is 1


## Understanding program

1. MAX_LIMIT is a constant till which the counting would happen
2. min(x,y) is the macro that would calculate the minimum value between x and y.
3. ta would count total time in interval of a.
4. tb would count total time in interval of b.
5. x is the variable interval that was mentioned in More Explanation.
6. Hopefully if else if part is self explanatory. (Just put words in code and it makes sense)
{"ft_lang_label":"__label__en","ft_lang_prob":0.87835133,"math_prob":0.9992117,"size":2160,"snap":"2019-51-2020-05","text_gpt3_token_len":724,"char_repetition_ratio":0.21243043,"word_repetition_ratio":0.1237525,"special_character_ratio":0.38425925,"punctuation_ratio":0.16453382,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99949396,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-08T03:57:36Z\",\"WARC-Record-ID\":\"<urn:uuid:2d12c7fd-6306-4433-9d3e-69bb876e8052>\",\"Content-Length\":\"68514\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fad677ee-3d8b-4550-8a78-3d0ea2215ccc>\",\"WARC-Concurrent-To\":\"<urn:uuid:abd24ed4-1132-47d9-8bcc-c7ed068cae49>\",\"WARC-IP-Address\":\"172.217.13.83\",\"WARC-Target-URI\":\"https://www.harishnote.com/2016/01/c-programming-challenge-09-counting-in.html\",\"WARC-Payload-Digest\":\"sha1:IECES6AUFVXBUBXVI5FMAZNFHN4B7BMD\",\"WARC-Block-Digest\":\"sha1:UL7FNPY74MCOPNMJ5DEBUHSUOFPEM2G6\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540504338.31_warc_CC-MAIN-20191208021121-20191208045121-00550.warc.gz\"}"}