← All problems
Problem 064
How many continued fractions for N ≤ 10000 have an odd period?
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(sqrt(n))
Space complexity O(1)O(1)
Approach Flow solution Continued fraction convergents
Verdict Suboptimal
Flow source
# Project Euler 064
# How many continued fractions for N ≤ 10000 have an odd period?
function is_square(n: i64) -> bool {
let mut r: i64 = 1
while r * r < n { r = r + 1 }
return r * r == n
}
function period(n: i64) -> i32 {
# continued fraction period of sqrt(n)
let a0: i64 = 0
let mut r: i64 = 1
while r * r <= n { r = r + 1 }
r = r - 1
# a0 = floor(sqrt(n))
let mut m: i64 = 0
let mut d: i64 = 1
let mut a: i64 = r
let mut period: i32 = 0
# repeat until a == 2*a0
while a != 2 * r {
m = d * a - m
d = (n - m * m) / d
a = (r + m) / d
period = period + 1
}
return period
}
function main() -> i32 {
let mut count: i64 = 0
for n in 2..10001 {
if !is_square(n) {
if period(n) % 2 == 1 {
count = count + 1
}
}
}
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_square_i64(int64_t n);
int32_t period_i64(int64_t n);
int32_t main(void);
bool is_square_i64(int64_t n) {
int64_t r = 1;
while ((r * r) < n) {
r = (r + 1);
}
return (r * r) == n;
}
int32_t period_i64(int64_t n) {
int64_t a0 = 0;
int64_t r = 1;
while ((r * r) <= n) {
r = (r + 1);
}
r = (r - 1);
int64_t m = 0;
int64_t d = 1;
int64_t a = r;
int32_t period = 0;
while (a != (2 * r)) {
m = ((d * a) - m);
d = FLOW_CHECKED_DIV(((n - (m * m))), (d));
a = FLOW_CHECKED_DIV(((r + m)), (d));
period = (period + 1);
}
return period;
}
int32_t main(void) {
int64_t count = 0;
int32_t __flow_step_1 = 1;
for (int32_t n = 2; (2 <= 10001) ? n < 10001 : n > 10001; n += (2 <= 10001) ? 1 : -1) {
if ((!(is_square_i64(n)))) {
if (FLOW_CHECKED_MOD((period_i64(n)), (2)) == 1) {
count = (count + 1);
}
}
}
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_square(%arg0: i64) -> i1 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%4 = llvm.load %3 : !llvm.ptr -> i64
%5 = llvm.load %3 : !llvm.ptr -> i64
%6 = arith.muli %4, %5 : i64
%7 = arith.cmpi slt, %6, %arg0 : i64
cf.cond_br %7, ^bb1, ^bb2
^bb1:
%8 = llvm.load %3 : !llvm.ptr -> i64
%9 = arith.constant 1 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.addi %8, %11 : i64
llvm.store %10, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%12 = llvm.load %3 : !llvm.ptr -> i64
%13 = llvm.load %3 : !llvm.ptr -> i64
%14 = arith.muli %12, %13 : i64
%15 = arith.cmpi eq, %14, %arg0 : i64
func.return %15 : i1
}
func.func @period(%arg0: i64) -> i32 {
%16 = arith.constant 0 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = arith.constant 1 : i32
%19 = arith.extsi %18 : i32 to i64
%20 = llvm.mlir.constant(1 : i64) : i64
%21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
llvm.store %19, %21 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%22 = llvm.load %21 : !llvm.ptr -> i64
%23 = llvm.load %21 : !llvm.ptr -> i64
%24 = arith.muli %22, %23 : i64
%25 = arith.cmpi sle, %24, %arg0 : i64
cf.cond_br %25, ^bb4, ^bb5
^bb4:
%26 = llvm.load %21 : !llvm.ptr -> i64
%27 = arith.constant 1 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.addi %26, %29 : i64
llvm.store %28, %21 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%30 = llvm.load %21 : !llvm.ptr -> i64
%31 = arith.constant 1 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.subi %30, %33 : i64
llvm.store %32, %21 : i64, !llvm.ptr
%34 = arith.constant 0 : i32
%35 = arith.extsi %34 : i32 to i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
%38 = arith.constant 1 : i32
%39 = arith.extsi %38 : i32 to i64
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i64, !llvm.ptr
%42 = llvm.load %21 : !llvm.ptr -> i64
%43 = llvm.mlir.constant(1 : i64) : i64
%44 = llvm.alloca %43 x i64 : (i64) -> !llvm.ptr
llvm.store %42, %44 : i64, !llvm.ptr
%45 = arith.constant 0 : i32
%46 = llvm.mlir.constant(1 : i64) : i64
%47 = llvm.alloca %46 x i32 : (i64) -> !llvm.ptr
llvm.store %45, %47 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%48 = llvm.load %44 : !llvm.ptr -> i64
%49 = arith.constant 2 : i32
%50 = llvm.load %21 : !llvm.ptr -> i64
%52 = arith.extsi %49 : i32 to i64
%51 = arith.muli %52, %50 : i64
%53 = arith.cmpi ne, %48, %51 : i64
cf.cond_br %53, ^bb7, ^bb8
^bb7:
%54 = llvm.load %41 : !llvm.ptr -> i64
%55 = llvm.load %44 : !llvm.ptr -> i64
%56 = arith.muli %54, %55 : i64
%57 = llvm.load %37 : !llvm.ptr -> i64
%58 = arith.subi %56, %57 : i64
llvm.store %58, %37 : i64, !llvm.ptr
%59 = llvm.load %37 : !llvm.ptr -> i64
%60 = llvm.load %37 : !llvm.ptr -> i64
%61 = arith.muli %59, %60 : i64
%62 = arith.subi %arg0, %61 : i64
%63 = llvm.load %41 : !llvm.ptr -> i64
%64 = arith.divsi %62, %63 : i64
llvm.store %64, %41 : i64, !llvm.ptr
%65 = llvm.load %21 : !llvm.ptr -> i64
%66 = llvm.load %37 : !llvm.ptr -> i64
%67 = arith.addi %65, %66 : i64
%68 = llvm.load %41 : !llvm.ptr -> i64
%69 = arith.divsi %67, %68 : i64
llvm.store %69, %44 : i64, !llvm.ptr
%70 = llvm.load %47 : !llvm.ptr -> i32
%71 = arith.constant 1 : i32
%72 = arith.addi %70, %71 : i32
llvm.store %72, %47 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%73 = llvm.load %47 : !llvm.ptr -> i32
func.return %73 : i32
}
func.func @main() -> i32 {
%74 = arith.constant 0 : i32
%75 = arith.extsi %74 : i32 to i64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i64, !llvm.ptr
%78 = arith.constant 2 : i32
%79 = arith.constant 10001 : i32
%80 = arith.index_cast %78 : i32 to index
%81 = arith.index_cast %79 : i32 to index
%83 = arith.constant 1 : index
%84 = arith.constant -1 : index
%85 = arith.cmpi sle, %80, %81 : index
%82 = arith.select %85, %83, %84 : index
cf.br ^bb9(%80 : index)
^bb9(%86: index):
%87 = arith.cmpi slt, %86, %81 : index
%88 = arith.cmpi sgt, %86, %81 : index
%89 = arith.select %85, %87, %88 : i1
cf.cond_br %89, ^bb10(%86 : index), ^bb11(%86 : index)
^bb10(%90: index):
%92 = arith.index_cast %90 : index to i64
%91 = func.call @is_square(%92) : (i64) -> i1
%94 = arith.constant 1 : i1
%93 = arith.xori %91, %94 : i1
cf.cond_br %93, ^bb12, ^bb13
^bb12:
%97 = arith.index_cast %90 : index to i64
%96 = func.call @period(%97) : (i64) -> i32
%98 = arith.constant 2 : i32
%99 = arith.remsi %96, %98 : i32
%100 = arith.constant 1 : i32
%101 = arith.cmpi eq, %99, %100 : i32
cf.cond_br %101, ^bb15, ^bb16
^bb15:
%102 = llvm.load %77 : !llvm.ptr -> i64
%103 = arith.constant 1 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.addi %102, %105 : i64
llvm.store %104, %77 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%106 = arith.addi %90, %82 : index
cf.br ^bb9(%106 : index)
^bb11(%107: index):
%108 = llvm.mlir.addressof @str_0 : !llvm.ptr
%109 = llvm.load %77 : !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
}
}