← All problems
Problem 704
S(N)=sum F(n); F(n)=floor(log2(n+1))-v2(n+1). Closed form.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 704
# S(N)=sum F(n); F(n)=floor(log2(n+1))-v2(n+1). Closed form.
function floor_log2(n: i64) -> i64 {
let mut x: i64 = n
let mut r: i64 = -1
while x > 0 {
x = x >> 1
r = r + 1
}
return r
}
function pow2(e: i64) -> i64 {
let mut r: i64 = 1
let mut i: i64 = 0
while i < e {
r = r * 2
i = i + 1
}
return r
}
function popcount(n0: i64) -> i64 {
let mut n: i64 = n0
let mut c: i64 = 0
while n > 0 {
c = c + (n & 1)
n = n >> 1
}
return c
}
function S(N: i64) -> i64 {
let L: i64 = floor_log2(N + 1)
let p2: i64 = pow2(L)
let sum_log: i64 = (N + 2) * L - 2 * (p2 - 1)
let sum_val: i64 = (N + 1) - popcount(N + 1)
return sum_log - sum_val
}
function main() -> i32 {
printf("%lld\n", S(10000000000000000))
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; }
int64_t floor_log2_i64(int64_t n);
int64_t pow2_i64(int64_t e);
int64_t popcount_i64(int64_t n0);
int64_t S_i64(int64_t N);
int32_t main(void);
int64_t floor_log2_i64(int64_t n) {
int64_t x = n;
int64_t r = (-1);
while (x > 0) {
x = FLOW_CHECKED_SHR((x), (1));
r = (r + 1);
}
return r;
}
int64_t pow2_i64(int64_t e) {
int64_t r = 1;
int64_t i = 0;
while (i < e) {
r = (r * 2);
i = (i + 1);
}
return r;
}
int64_t popcount_i64(int64_t n0) {
int64_t n = n0;
int64_t c = 0;
while (n > 0) {
c = (c + (n & 1));
n = FLOW_CHECKED_SHR((n), (1));
}
return c;
}
int64_t S_i64(int64_t N) {
int64_t L = floor_log2_i64((N + 1));
int64_t p2 = pow2_i64(L);
int64_t sum_log = (((N + 2) * L) - (2 * (p2 - 1)));
int64_t sum_val = ((N + 1) - popcount_i64((N + 1)));
return (sum_log - sum_val);
}
int32_t main(void) {
printf("%lld\n", S_i64(10000000000000000));
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 @floor_log2(%arg0: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = arith.constant 1 : i32
%4 = arith.constant 0 : i32
%3 = arith.subi %4, %2 : i32
%5 = arith.extsi %3 : i32 to i64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%8 = llvm.load %1 : !llvm.ptr -> i64
%9 = arith.constant 0 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.cmpi sgt, %8, %11 : i64
cf.cond_br %10, ^bb1, ^bb2
^bb1:
%12 = llvm.load %1 : !llvm.ptr -> i64
%13 = arith.constant 1 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.shrsi %12, %15 : i64
llvm.store %14, %1 : i64, !llvm.ptr
%16 = llvm.load %7 : !llvm.ptr -> i64
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.addi %16, %19 : i64
llvm.store %18, %7 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%20 = llvm.load %7 : !llvm.ptr -> i64
func.return %20 : i64
}
func.func @pow2(%arg0: i64) -> i64 {
%21 = arith.constant 1 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i64, !llvm.ptr
%25 = arith.constant 0 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%29 = llvm.load %28 : !llvm.ptr -> i64
%30 = arith.cmpi slt, %29, %arg0 : i64
cf.cond_br %30, ^bb4, ^bb5
^bb4:
%31 = llvm.load %24 : !llvm.ptr -> i64
%32 = arith.constant 2 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.muli %31, %34 : i64
llvm.store %33, %24 : i64, !llvm.ptr
%35 = llvm.load %28 : !llvm.ptr -> i64
%36 = arith.constant 1 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.addi %35, %38 : i64
llvm.store %37, %28 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%39 = llvm.load %24 : !llvm.ptr -> i64
func.return %39 : i64
}
func.func @popcount(%arg0: i64) -> i64 {
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %41 : i64, !llvm.ptr
%42 = arith.constant 0 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%46 = llvm.load %41 : !llvm.ptr -> i64
%47 = arith.constant 0 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.cmpi sgt, %46, %49 : i64
cf.cond_br %48, ^bb7, ^bb8
^bb7:
%50 = llvm.load %45 : !llvm.ptr -> i64
%51 = llvm.load %41 : !llvm.ptr -> i64
%52 = arith.constant 1 : i32
%54 = arith.extsi %52 : i32 to i64
%53 = arith.andi %51, %54 : i64
%55 = arith.addi %50, %53 : i64
llvm.store %55, %45 : i64, !llvm.ptr
%56 = llvm.load %41 : !llvm.ptr -> i64
%57 = arith.constant 1 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.shrsi %56, %59 : i64
llvm.store %58, %41 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%60 = llvm.load %45 : !llvm.ptr -> i64
func.return %60 : i64
}
func.func @S(%arg0: i64) -> i64 {
%62 = arith.constant 1 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.addi %arg0, %64 : i64
%61 = func.call @floor_log2(%63) : (i64) -> i64
%65 = func.call @pow2(%61) : (i64) -> i64
%66 = arith.constant 2 : i32
%68 = arith.extsi %66 : i32 to i64
%67 = arith.addi %arg0, %68 : i64
%69 = arith.muli %67, %61 : i64
%70 = arith.constant 2 : i32
%71 = arith.constant 1 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.subi %65, %73 : i64
%75 = arith.extsi %70 : i32 to i64
%74 = arith.muli %75, %72 : i64
%76 = arith.subi %69, %74 : i64
%77 = arith.constant 1 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.addi %arg0, %79 : i64
%81 = arith.constant 1 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %arg0, %83 : i64
%80 = func.call @popcount(%82) : (i64) -> i64
%84 = arith.subi %78, %80 : i64
%85 = arith.subi %76, %84 : i64
func.return %85 : i64
}
func.func @main() -> i32 {
%86 = llvm.mlir.addressof @str_0 : !llvm.ptr
%88 = arith.constant 9999995705032704 : i32
%89 = arith.extsi %88 : i32 to i64
%87 = func.call @S(%89) : (i64) -> i64
%90 = llvm.call @printf(%86, %87) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%91 = arith.constant 0 : i32
func.return %91 : i32
}
}