Function, which returns the next 50 leap years

I need to create a function that would return the next 50 leap years beginning from the next leap year after the input year in R. The problem is that I need to save it as a vector.

``````x <- function(year){
year1 <- year + 205
for(i in year:year1){
if((i %% 4 == 0) & (i %% 100 != 0) | (i  %% 400 == 0)){
print(i)
}
next
}
}
```
```

In this function I created the vector for years which would include at least the next 50 leap years, but I guess, there is a way where R just closes the loop when it has 50 values in a vector. Thanks!

You can do:

``````leap50 <- function(y) {
y <- y - y %% 4 + 4
y <- y + 4*(0:60)
y[(y %% 100) !=0 | (y %% 400)==0][1:50]
}
leap50(2016)
leap50(2017)
leap50(2316)
# > leap50(2016)
# [1] 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092
# [20] 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172
# [39] 2176 2180 2184 2188 2192 2196 2204 2208 2212 2216 2220 2224
# > leap50(2017)
# [1] 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092
# [20] 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172
# [39] 2176 2180 2184 2188 2192 2196 2204 2208 2212 2216 2220 2224
# > leap50(2316)
# [1] 2320 2324 2328 2332 2336 2340 2344 2348 2352 2356 2360 2364 2368 2372 2376 2380 2384 2388 2392
# [20] 2396 2400 2404 2408 2412 2416 2420 2424 2428 2432 2436 2440 2444 2448 2452 2456 2460 2464 2468
# [39] 2472 2476 2480 2484 2488 2492 2496 2504 2508 2512 2516 2520
```
```