← All problems
Problem 019
How many Sundays fell on the first of the month during the 20th century (1 Jan 1901 to 31 Dec 2000)? 1 Jan 1900 was a Monday.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(1)
Space complexity O(1)O(1)
Approach Flow solution Zeller congruence or direct count
Verdict Suboptimal
Flow source
# Project Euler 019
# How many Sundays fell on the first of the month during the 20th century
# (1 Jan 1901 to 31 Dec 2000)?
# 1 Jan 1900 was a Monday.
function is_leap(y: i32) -> bool {
if y % 400 == 0 {
return true
}
if y % 100 == 0 {
return false
}
return y % 4 == 0
}
function days_in_month(y: i32, m: i32) -> i32 {
if m == 2 {
if is_leap(y) {
return 29
}
return 28
}
if m == 4 || m == 6 || m == 9 || m == 11 {
return 30
}
return 31
}
function main() -> i32 {
# day-of-week: 0 = Monday ... 6 = Sunday, starting 1 Jan 1900
let mut dow: i32 = 0
for m in 1..13 {
dow = (dow + days_in_month(1900, m)) % 7
}
let mut count: i64 = 0
for y in 1901..2001 {
for m in 1..13 {
if dow == 6 {
count = count + 1
}
dow = (dow + days_in_month(y, m)) % 7
}
}
printf("%lld\n", count)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
bool is_leap_i32(int32_t y);
int32_t days_in_month_i32_i32(int32_t y, int32_t m);
int32_t main(void);
bool is_leap_i32(int32_t y) {
if (FLOW_CHECKED_MOD((y), (400)) == 0) {
return 1;
}
if (FLOW_CHECKED_MOD((y), (100)) == 0) {
return 0;
}
return FLOW_CHECKED_MOD((y), (4)) == 0;
}
int32_t days_in_month_i32_i32(int32_t y, int32_t m) {
if (m == 2) {
if (is_leap_i32(y)) {
return 29;
}
return 28;
}
if ((((m == 4 || m == 6) || m == 9) || m == 11)) {
return 30;
}
return 31;
}
int32_t main(void) {
int32_t dow = 0;
int32_t __flow_step_1 = 1;
for (int32_t m = 1; (1 <= 13) ? m < 13 : m > 13; m += (1 <= 13) ? 1 : -1) {
dow = FLOW_CHECKED_MOD(((dow + days_in_month_i32_i32(1900, m))), (7));
}
int64_t count = 0;
int32_t __flow_step_2 = 1;
for (int32_t y = 1901; (1901 <= 2001) ? y < 2001 : y > 2001; y += (1901 <= 2001) ? 1 : -1) {
int32_t __flow_step_3 = 1;
for (int32_t m = 1; (1 <= 13) ? m < 13 : m > 13; m += (1 <= 13) ? 1 : -1) {
if (dow == 6) {
count = (count + 1);
}
dow = FLOW_CHECKED_MOD(((dow + days_in_month_i32_i32(y, m))), (7));
}
}
printf("%lld\n", count);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func @is_leap(%arg0: i32) -> i1 {
%0 = arith.constant 400 : i32
%1 = arith.remsi %arg0, %0 : i32
%2 = arith.constant 0 : i32
%3 = arith.cmpi eq, %1, %2 : i32
cf.cond_br %3, ^bb0, ^bb1
^bb0:
%4 = arith.constant 1 : i1
func.return %4 : i1
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 100 : i32
%6 = arith.remsi %arg0, %5 : i32
%7 = arith.constant 0 : i32
%8 = arith.cmpi eq, %6, %7 : i32
cf.cond_br %8, ^bb3, ^bb4
^bb3:
%9 = arith.constant 0 : i1
func.return %9 : i1
^bb4:
cf.br ^bb5
^bb5:
%10 = arith.constant 4 : i32
%11 = arith.remsi %arg0, %10 : i32
%12 = arith.constant 0 : i32
%13 = arith.cmpi eq, %11, %12 : i32
func.return %13 : i1
}
func.func @days_in_month(%arg0: i32, %arg1: i32) -> i32 {
%14 = arith.constant 2 : i32
%15 = arith.cmpi eq, %arg1, %14 : i32
cf.cond_br %15, ^bb6, ^bb7
^bb6:
%16 = func.call @is_leap(%arg0) : (i32) -> i1
cf.cond_br %16, ^bb9, ^bb10
^bb9:
%17 = arith.constant 29 : i32
func.return %17 : i32
^bb10:
cf.br ^bb11
^bb11:
%18 = arith.constant 28 : i32
func.return %18 : i32
^bb7:
cf.br ^bb8
^bb8:
%19 = arith.constant 4 : i32
%20 = arith.cmpi eq, %arg1, %19 : i32
%21 = scf.if %20 -> (i1) {
%22 = arith.constant true
scf.yield %22 : i1
} else {
%23 = arith.constant 6 : i32
%24 = arith.cmpi eq, %arg1, %23 : i32
scf.yield %24 : i1
}
%25 = scf.if %21 -> (i1) {
%26 = arith.constant true
scf.yield %26 : i1
} else {
%27 = arith.constant 9 : i32
%28 = arith.cmpi eq, %arg1, %27 : i32
scf.yield %28 : i1
}
%29 = scf.if %25 -> (i1) {
%30 = arith.constant true
scf.yield %30 : i1
} else {
%31 = arith.constant 11 : i32
%32 = arith.cmpi eq, %arg1, %31 : i32
scf.yield %32 : i1
}
cf.cond_br %29, ^bb12, ^bb13
^bb12:
%33 = arith.constant 30 : i32
func.return %33 : i32
^bb13:
cf.br ^bb14
^bb14:
%34 = arith.constant 31 : i32
func.return %34 : i32
}
func.func @main() -> i32 {
%35 = arith.constant 0 : i32
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i32 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i32, !llvm.ptr
%38 = arith.constant 1 : i32
%39 = arith.constant 13 : i32
%40 = arith.index_cast %38 : i32 to index
%41 = arith.index_cast %39 : i32 to index
%43 = arith.constant 1 : index
%44 = arith.constant -1 : index
%45 = arith.cmpi sle, %40, %41 : index
%42 = arith.select %45, %43, %44 : index
cf.br ^bb15(%40 : index)
^bb15(%46: index):
%47 = arith.cmpi slt, %46, %41 : index
%48 = arith.cmpi sgt, %46, %41 : index
%49 = arith.select %45, %47, %48 : i1
cf.cond_br %49, ^bb16(%46 : index), ^bb17(%46 : index)
^bb16(%50: index):
%51 = llvm.load %37 : !llvm.ptr -> i32
%53 = arith.constant 1900 : i32
%54 = arith.index_cast %50 : index to i32
%52 = func.call @days_in_month(%53, %54) : (i32, i32) -> i32
%55 = arith.addi %51, %52 : i32
%56 = arith.constant 7 : i32
%57 = arith.remsi %55, %56 : i32
llvm.store %57, %37 : i32, !llvm.ptr
%58 = arith.addi %50, %42 : index
cf.br ^bb15(%58 : index)
^bb17(%59: index):
%60 = arith.constant 0 : i32
%61 = arith.extsi %60 : i32 to i64
%62 = llvm.mlir.constant(1 : i64) : i64
%63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
llvm.store %61, %63 : i64, !llvm.ptr
%64 = arith.constant 1901 : i32
%65 = arith.constant 2001 : i32
%66 = arith.index_cast %64 : i32 to index
%67 = arith.index_cast %65 : i32 to index
%69 = arith.constant 1 : index
%70 = arith.constant -1 : index
%71 = arith.cmpi sle, %66, %67 : index
%68 = arith.select %71, %69, %70 : index
cf.br ^bb18(%66 : index)
^bb18(%72: index):
%73 = arith.cmpi slt, %72, %67 : index
%74 = arith.cmpi sgt, %72, %67 : index
%75 = arith.select %71, %73, %74 : i1
cf.cond_br %75, ^bb19(%72 : index), ^bb20(%72 : index)
^bb19(%76: index):
%77 = arith.constant 1 : i32
%78 = arith.constant 13 : i32
%79 = arith.index_cast %77 : i32 to index
%80 = arith.index_cast %78 : i32 to index
%82 = arith.constant 1 : index
%83 = arith.constant -1 : index
%84 = arith.cmpi sle, %79, %80 : index
%81 = arith.select %84, %82, %83 : index
cf.br ^bb21(%79 : index)
^bb21(%85: index):
%86 = arith.cmpi slt, %85, %80 : index
%87 = arith.cmpi sgt, %85, %80 : index
%88 = arith.select %84, %86, %87 : i1
cf.cond_br %88, ^bb22(%85 : index), ^bb23(%85 : index)
^bb22(%89: index):
%90 = llvm.load %37 : !llvm.ptr -> i32
%91 = arith.constant 6 : i32
%92 = arith.cmpi eq, %90, %91 : i32
cf.cond_br %92, ^bb24, ^bb25
^bb24:
%93 = llvm.load %63 : !llvm.ptr -> i64
%94 = arith.constant 1 : i32
%96 = arith.extsi %94 : i32 to i64
%95 = arith.addi %93, %96 : i64
llvm.store %95, %63 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%97 = llvm.load %37 : !llvm.ptr -> i32
%99 = arith.index_cast %76 : index to i32
%100 = arith.index_cast %89 : index to i32
%98 = func.call @days_in_month(%99, %100) : (i32, i32) -> i32
%101 = arith.addi %97, %98 : i32
%102 = arith.constant 7 : i32
%103 = arith.remsi %101, %102 : i32
llvm.store %103, %37 : i32, !llvm.ptr
%104 = arith.addi %89, %81 : index
cf.br ^bb21(%104 : index)
^bb23(%105: index):
%106 = arith.addi %76, %68 : index
cf.br ^bb18(%106 : index)
^bb20(%107: index):
%108 = llvm.mlir.addressof @str_0 : !llvm.ptr
%109 = llvm.load %63 : !llvm.ptr -> i64
%110 = llvm.call @printf(%108, %109) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%111 = arith.constant 0 : i32
func.return %111 : i32
}
}