← All problems
Problem 582
Nearly Isosceles 120 Degree Triangles Count (a,b,c) with 120° angle, a<=b<=c, b-a<=100, c<=10^100 via orbits of x^2 - 3y^2 = k^2 with x=2c, y=2a+k, b=a+k.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(1)O(n^2)
Approach Flow solution Bottom-up DP
Verdict Optimal
Flow source
# Project Euler 582
# Nearly Isosceles 120 Degree Triangles
# Count (a,b,c) with 120° angle, a<=b<=c, b-a<=100, c<=10^100
# via orbits of x^2 - 3y^2 = k^2 with x=2c, y=2a+k, b=a+k.
extern {
function log(x: f64) -> f64
function sqrt(x: f64) -> f64
}
function isqrt(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function is_square(n: i64) -> i64 {
if n < 0 { return -1 }
let r: i64 = isqrt(n)
if r * r == n { return r }
return -1
}
function count_orbit(x0: i64, y0: i64, k: i64, even_k: bool, limit_log: f64) -> i64 {
let mut x: i64 = x0
let mut y: i64 = y0
let mut total: i64 = 0
let max_safe: i64 = 2000000000000000000
let mut growth: f64 = 2.0 + sqrt(3.0)
if not even_k {
growth = 7.0 + 4.0 * sqrt(3.0)
}
let log_g: f64 = log(growth)
while true {
let mut nx: i64 = 0
let mut ny: i64 = 0
let mut overflow: bool = false
if even_k {
if x > max_safe / 2 {
overflow = true
} else {
nx = 2 * x + 3 * y
ny = x + 2 * y
if nx < x { overflow = true }
}
} else {
if x > max_safe / 7 {
overflow = true
} else {
nx = 7 * x + 12 * y
ny = 4 * x + 7 * y
if nx < x { overflow = true }
}
}
if overflow {
let mut lx: f64 = log(x as f64)
while lx <= limit_log + 0.000000000001 {
if y > k {
total = total + 1
}
y = k + 1
x = max_safe
lx = lx + log_g
}
break
}
let lx: f64 = log(x as f64)
if lx > limit_log + 0.000000000001 {
break
}
if y > k {
total = total + 1
}
x = nx
y = ny
}
return total
}
function count_triangles() -> i64 {
let limit_log: f64 = log(2.0) + 100.0 * log(10.0)
let mut total: i64 = 0
let y_max: i64 = 2000
let mut k: i64 = 1
while k <= 100 {
let k2: i64 = k * k
let even_k: bool = (k % 2) == 0
let mut y: i64 = 0
while y <= y_max {
if ((y - k) & 1) == 0 {
let x: i64 = is_square(3 * y * y + k2)
if x >= 0 && (x & 1) == 0 {
let mut xp: i64 = 0
let mut yp: i64 = 0
if even_k {
xp = 2 * x - 3 * y
yp = 2 * y - x
} else {
xp = 7 * x - 12 * y
yp = 7 * y - 4 * x
}
let mut is_seed: bool = true
if xp > 0 && yp >= 0 && ((yp - k) & 1) == 0 && (xp & 1) == 0 {
if xp * xp - 3 * yp * yp == k2 {
is_seed = false
}
}
if is_seed {
total = total + count_orbit(x, y, k, even_k, limit_log)
}
}
}
y = y + 1
}
k = k + 1
}
return total
}
function main() -> i32 {
printf("%lld\n", count_triangles())
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 isqrt_i64(int64_t n);
int64_t is_square_i64(int64_t n);
int64_t count_orbit_i64_i64_i64_bool_f64(int64_t x0, int64_t y0, int64_t k, bool even_k, double limit_log);
int64_t count_triangles(void);
int32_t main(void);
int64_t isqrt_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t is_square_i64(int64_t n) {
if (n < 0) {
return (-1);
}
int64_t r = isqrt_i64(n);
if ((r * r) == n) {
return r;
}
return (-1);
}
int64_t count_orbit_i64_i64_i64_bool_f64(int64_t x0, int64_t y0, int64_t k, bool even_k, double limit_log) {
int64_t x = x0;
int64_t y = y0;
int64_t total = 0;
int64_t max_safe = 2000000000000000000;
double growth = (2.0 + sqrt(3.0));
if ((!(even_k))) {
growth = (7.0 + (4.0 * sqrt(3.0)));
}
double log_g = log(growth);
while (1) {
int64_t nx = 0;
int64_t ny = 0;
bool overflow = 0;
if (even_k) {
if (x > FLOW_CHECKED_DIV((max_safe), (2))) {
overflow = 1;
} else {
nx = ((2 * x) + (3 * y));
ny = (x + (2 * y));
if (nx < x) {
overflow = 1;
}
}
} else {
if (x > FLOW_CHECKED_DIV((max_safe), (7))) {
overflow = 1;
} else {
nx = ((7 * x) + (12 * y));
ny = ((4 * x) + (7 * y));
if (nx < x) {
overflow = 1;
}
}
}
if (overflow) {
double lx = log(((double)(x)));
while (lx <= (limit_log + 0.000000000001)) {
if (y > k) {
total = (total + 1);
}
y = (k + 1);
x = max_safe;
lx = (lx + log_g);
}
break;
}
double lx = log(((double)(x)));
if (lx > (limit_log + 0.000000000001)) {
break;
}
if (y > k) {
total = (total + 1);
}
x = nx;
y = ny;
}
return total;
}
int64_t count_triangles(void) {
double limit_log = (log(2.0) + (100.0 * log(10.0)));
int64_t total = 0;
int64_t y_max = 2000;
int64_t k = 1;
while (k <= 100) {
int64_t k2 = (k * k);
bool even_k = FLOW_CHECKED_MOD((k), (2)) == 0;
int64_t y = 0;
while (y <= y_max) {
if (((y - k) & 1) == 0) {
int64_t x = is_square_i64((((3 * y) * y) + k2));
if ((x >= 0 && (x & 1) == 0)) {
int64_t xp = 0;
int64_t yp = 0;
if (even_k) {
xp = ((2 * x) - (3 * y));
yp = ((2 * y) - x);
} else {
xp = ((7 * x) - (12 * y));
yp = ((7 * y) - (4 * x));
}
bool is_seed = 1;
if ((((xp > 0 && yp >= 0) && ((yp - k) & 1) == 0) && (xp & 1) == 0)) {
if (((xp * xp) - ((3 * yp) * yp)) == k2) {
is_seed = 0;
}
}
if (is_seed) {
total = (total + count_orbit_i64_i64_i64_bool_f64(x, y, k, even_k, limit_log));
}
}
}
y = (y + 1);
}
k = (k + 1);
}
return total;
}
int32_t main(void) {
printf("%lld\n", count_triangles());
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 private @log(f64) -> f64
func.func private @sqrt(f64) -> f64
func.func @isqrt(%arg0: i64) -> i64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi sle, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%4 = arith.extsi %3 : i32 to i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %6 : i64, !llvm.ptr
%7 = llvm.load %6 : !llvm.ptr -> i64
%8 = arith.constant 1 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.addi %7, %10 : i64
%11 = arith.constant 2 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.divsi %9, %13 : i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %12, %15 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = llvm.load %6 : !llvm.ptr -> i64
%18 = arith.cmpi slt, %16, %17 : i64
cf.cond_br %18, ^bb4, ^bb5
^bb4:
%19 = llvm.load %15 : !llvm.ptr -> i64
llvm.store %19, %6 : i64, !llvm.ptr
%20 = llvm.load %6 : !llvm.ptr -> i64
%21 = llvm.load %6 : !llvm.ptr -> i64
%22 = arith.divsi %arg0, %21 : i64
%23 = arith.addi %20, %22 : i64
%24 = arith.constant 2 : i32
%26 = arith.extsi %24 : i32 to i64
%25 = arith.divsi %23, %26 : i64
llvm.store %25, %15 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%27 = llvm.load %6 : !llvm.ptr -> i64
func.return %27 : i64
}
func.func @is_square(%arg0: i64) -> i64 {
%28 = arith.constant 0 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.cmpi slt, %arg0, %30 : i64
cf.cond_br %29, ^bb6, ^bb7
^bb6:
%31 = arith.constant 1 : i32
%33 = arith.constant 0 : i32
%32 = arith.subi %33, %31 : i32
%34 = arith.extsi %32 : i32 to i64
func.return %34 : i64
^bb7:
cf.br ^bb8
^bb8:
%35 = func.call @isqrt(%arg0) : (i64) -> i64
%36 = arith.muli %35, %35 : i64
%37 = arith.cmpi eq, %36, %arg0 : i64
cf.cond_br %37, ^bb9, ^bb10
^bb9:
func.return %35 : i64
^bb10:
cf.br ^bb11
^bb11:
%38 = arith.constant 1 : i32
%40 = arith.constant 0 : i32
%39 = arith.subi %40, %38 : i32
%41 = arith.extsi %39 : i32 to i64
func.return %41 : i64
}
func.func @count_orbit(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i1, %arg4: f64) -> i64 {
%42 = llvm.mlir.constant(1 : i64) : i64
%43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %43 : i64, !llvm.ptr
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %45 : i64, !llvm.ptr
%46 = arith.constant 0 : i32
%47 = arith.extsi %46 : i32 to i64
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
llvm.store %47, %49 : i64, !llvm.ptr
%50 = arith.constant 1999999995705032704 : i32
%51 = arith.extsi %50 : i32 to i64
%52 = arith.constant 2.0 : f32
%53 = arith.constant 3.0 : f32
%54 = math.sqrt %53 : f32
%56 = arith.extf %52 : f32 to f64
%55 = arith.addf %56, %54 : f64
%57 = llvm.mlir.constant(1 : i64) : i64
%58 = llvm.alloca %57 x f64 : (i64) -> !llvm.ptr
llvm.store %55, %58 : f64, !llvm.ptr
%60 = arith.constant 1 : i1
%59 = arith.xori %arg3, %60 : i1
cf.cond_br %59, ^bb12, ^bb13
^bb12:
%62 = arith.constant 7.0 : f32
%63 = arith.constant 4.0 : f32
%64 = arith.constant 3.0 : f32
%65 = math.sqrt %64 : f32
%67 = arith.extf %63 : f32 to f64
%66 = arith.mulf %67, %65 : f64
%69 = arith.extf %62 : f32 to f64
%68 = arith.addf %69, %66 : f64
llvm.store %68, %58 : f64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%70 = llvm.load %58 : !llvm.ptr -> f64
%71 = math.log %70 : f64
cf.br ^bb15
^bb15:
%72 = arith.constant 1 : i1
cf.cond_br %72, ^bb16, ^bb17
^bb16:
%73 = arith.constant 0 : i32
%74 = arith.extsi %73 : i32 to i64
%75 = llvm.mlir.constant(1 : i64) : i64
%76 = llvm.alloca %75 x i64 : (i64) -> !llvm.ptr
llvm.store %74, %76 : i64, !llvm.ptr
%77 = arith.constant 0 : i32
%78 = arith.extsi %77 : i32 to i64
%79 = llvm.mlir.constant(1 : i64) : i64
%80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
llvm.store %78, %80 : i64, !llvm.ptr
%81 = arith.constant 0 : i1
%82 = llvm.mlir.constant(1 : i64) : i64
%83 = llvm.alloca %82 x i1 : (i64) -> !llvm.ptr
llvm.store %81, %83 : i1, !llvm.ptr
cf.cond_br %arg3, ^bb18, ^bb19
^bb18:
%84 = llvm.load %43 : !llvm.ptr -> i64
%85 = arith.constant 2 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.divsi %51, %87 : i64
%88 = arith.cmpi sgt, %84, %86 : i64
cf.cond_br %88, ^bb21, ^bb22
^bb21:
%89 = arith.constant 1 : i1
llvm.store %89, %83 : i1, !llvm.ptr
cf.br ^bb23
^bb22:
%90 = arith.constant 2 : i32
%91 = llvm.load %43 : !llvm.ptr -> i64
%93 = arith.extsi %90 : i32 to i64
%92 = arith.muli %93, %91 : i64
%94 = arith.constant 3 : i32
%95 = llvm.load %45 : !llvm.ptr -> i64
%97 = arith.extsi %94 : i32 to i64
%96 = arith.muli %97, %95 : i64
%98 = arith.addi %92, %96 : i64
llvm.store %98, %76 : i64, !llvm.ptr
%99 = llvm.load %43 : !llvm.ptr -> i64
%100 = arith.constant 2 : i32
%101 = llvm.load %45 : !llvm.ptr -> i64
%103 = arith.extsi %100 : i32 to i64
%102 = arith.muli %103, %101 : i64
%104 = arith.addi %99, %102 : i64
llvm.store %104, %80 : i64, !llvm.ptr
%105 = llvm.load %76 : !llvm.ptr -> i64
%106 = llvm.load %43 : !llvm.ptr -> i64
%107 = arith.cmpi slt, %105, %106 : i64
cf.cond_br %107, ^bb24, ^bb25
^bb24:
%108 = arith.constant 1 : i1
llvm.store %108, %83 : i1, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
cf.br ^bb23
^bb23:
cf.br ^bb20
^bb19:
%109 = llvm.load %43 : !llvm.ptr -> i64
%110 = arith.constant 7 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.divsi %51, %112 : i64
%113 = arith.cmpi sgt, %109, %111 : i64
cf.cond_br %113, ^bb27, ^bb28
^bb27:
%114 = arith.constant 1 : i1
llvm.store %114, %83 : i1, !llvm.ptr
cf.br ^bb29
^bb28:
%115 = arith.constant 7 : i32
%116 = llvm.load %43 : !llvm.ptr -> i64
%118 = arith.extsi %115 : i32 to i64
%117 = arith.muli %118, %116 : i64
%119 = arith.constant 12 : i32
%120 = llvm.load %45 : !llvm.ptr -> i64
%122 = arith.extsi %119 : i32 to i64
%121 = arith.muli %122, %120 : i64
%123 = arith.addi %117, %121 : i64
llvm.store %123, %76 : i64, !llvm.ptr
%124 = arith.constant 4 : i32
%125 = llvm.load %43 : !llvm.ptr -> i64
%127 = arith.extsi %124 : i32 to i64
%126 = arith.muli %127, %125 : i64
%128 = arith.constant 7 : i32
%129 = llvm.load %45 : !llvm.ptr -> i64
%131 = arith.extsi %128 : i32 to i64
%130 = arith.muli %131, %129 : i64
%132 = arith.addi %126, %130 : i64
llvm.store %132, %80 : i64, !llvm.ptr
%133 = llvm.load %76 : !llvm.ptr -> i64
%134 = llvm.load %43 : !llvm.ptr -> i64
%135 = arith.cmpi slt, %133, %134 : i64
cf.cond_br %135, ^bb30, ^bb31
^bb30:
%136 = arith.constant 1 : i1
llvm.store %136, %83 : i1, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
cf.br ^bb29
^bb29:
cf.br ^bb20
^bb20:
%137 = llvm.load %83 : !llvm.ptr -> i1
cf.cond_br %137, ^bb33, ^bb34
^bb33:
%138 = llvm.load %43 : !llvm.ptr -> i64
%139 = arith.sitofp %138 : i64 to f64
%140 = math.log %139 : f64
%141 = llvm.mlir.constant(1 : i64) : i64
%142 = llvm.alloca %141 x f64 : (i64) -> !llvm.ptr
llvm.store %140, %142 : f64, !llvm.ptr
cf.br ^bb36
^bb36:
%143 = llvm.load %142 : !llvm.ptr -> f64
%144 = arith.constant 0.000000000001 : f32
%146 = arith.extf %144 : f32 to f64
%145 = arith.addf %arg4, %146 : f64
%147 = arith.cmpf ole, %143, %145 : f64
cf.cond_br %147, ^bb37, ^bb38
^bb37:
%148 = llvm.load %45 : !llvm.ptr -> i64
%149 = arith.cmpi sgt, %148, %arg2 : i64
cf.cond_br %149, ^bb39, ^bb40
^bb39:
%150 = llvm.load %49 : !llvm.ptr -> i64
%151 = arith.constant 1 : i32
%153 = arith.extsi %151 : i32 to i64
%152 = arith.addi %150, %153 : i64
llvm.store %152, %49 : i64, !llvm.ptr
cf.br ^bb41
^bb40:
cf.br ^bb41
^bb41:
%154 = arith.constant 1 : i32
%156 = arith.extsi %154 : i32 to i64
%155 = arith.addi %arg2, %156 : i64
llvm.store %155, %45 : i64, !llvm.ptr
llvm.store %51, %43 : i64, !llvm.ptr
%157 = llvm.load %142 : !llvm.ptr -> f64
%158 = arith.addf %157, %71 : f64
llvm.store %158, %142 : f64, !llvm.ptr
cf.br ^bb36
^bb38:
cf.br ^bb17
^bb34:
cf.br ^bb35
^bb35:
%159 = llvm.load %43 : !llvm.ptr -> i64
%160 = arith.sitofp %159 : i64 to f64
%161 = math.log %160 : f64
%162 = arith.constant 0.000000000001 : f32
%164 = arith.extf %162 : f32 to f64
%163 = arith.addf %arg4, %164 : f64
%165 = arith.cmpf ogt, %161, %163 : f64
cf.cond_br %165, ^bb42, ^bb43
^bb42:
cf.br ^bb17
^bb43:
cf.br ^bb44
^bb44:
%166 = llvm.load %45 : !llvm.ptr -> i64
%167 = arith.cmpi sgt, %166, %arg2 : i64
cf.cond_br %167, ^bb45, ^bb46
^bb45:
%168 = llvm.load %49 : !llvm.ptr -> i64
%169 = arith.constant 1 : i32
%171 = arith.extsi %169 : i32 to i64
%170 = arith.addi %168, %171 : i64
llvm.store %170, %49 : i64, !llvm.ptr
cf.br ^bb47
^bb46:
cf.br ^bb47
^bb47:
%172 = llvm.load %76 : !llvm.ptr -> i64
llvm.store %172, %43 : i64, !llvm.ptr
%173 = llvm.load %80 : !llvm.ptr -> i64
llvm.store %173, %45 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%174 = llvm.load %49 : !llvm.ptr -> i64
func.return %174 : i64
}
func.func @count_triangles() -> i64 {
%175 = arith.constant 2.0 : f32
%176 = math.log %175 : f32
%177 = arith.constant 100.0 : f32
%178 = arith.constant 10.0 : f32
%179 = math.log %178 : f32
%181 = arith.extf %177 : f32 to f64
%180 = arith.mulf %181, %179 : f64
%182 = arith.addf %176, %180 : f64
%183 = arith.constant 0 : i32
%184 = arith.extsi %183 : i32 to i64
%185 = llvm.mlir.constant(1 : i64) : i64
%186 = llvm.alloca %185 x i64 : (i64) -> !llvm.ptr
llvm.store %184, %186 : i64, !llvm.ptr
%187 = arith.constant 2000 : i32
%188 = arith.extsi %187 : i32 to i64
%189 = arith.constant 1 : i32
%190 = arith.extsi %189 : i32 to i64
%191 = llvm.mlir.constant(1 : i64) : i64
%192 = llvm.alloca %191 x i64 : (i64) -> !llvm.ptr
llvm.store %190, %192 : i64, !llvm.ptr
cf.br ^bb48
^bb48:
%193 = llvm.load %192 : !llvm.ptr -> i64
%194 = arith.constant 100 : i32
%196 = arith.extsi %194 : i32 to i64
%195 = arith.cmpi sle, %193, %196 : i64
cf.cond_br %195, ^bb49, ^bb50
^bb49:
%197 = llvm.load %192 : !llvm.ptr -> i64
%198 = llvm.load %192 : !llvm.ptr -> i64
%199 = arith.muli %197, %198 : i64
%200 = llvm.load %192 : !llvm.ptr -> i64
%201 = arith.constant 2 : i32
%203 = arith.extsi %201 : i32 to i64
%202 = arith.remsi %200, %203 : i64
%204 = arith.constant 0 : i32
%206 = arith.extsi %204 : i32 to i64
%205 = arith.cmpi eq, %202, %206 : i64
%207 = arith.constant 0 : i32
%208 = arith.extsi %207 : i32 to i64
%209 = llvm.mlir.constant(1 : i64) : i64
%210 = llvm.alloca %209 x i64 : (i64) -> !llvm.ptr
llvm.store %208, %210 : i64, !llvm.ptr
cf.br ^bb51
^bb51:
%211 = llvm.load %210 : !llvm.ptr -> i64
%212 = arith.cmpi sle, %211, %188 : i64
cf.cond_br %212, ^bb52, ^bb53
^bb52:
%213 = llvm.load %210 : !llvm.ptr -> i64
%214 = llvm.load %192 : !llvm.ptr -> i64
%215 = arith.subi %213, %214 : i64
%216 = arith.constant 1 : i32
%218 = arith.extsi %216 : i32 to i64
%217 = arith.andi %215, %218 : i64
%219 = arith.constant 0 : i32
%221 = arith.extsi %219 : i32 to i64
%220 = arith.cmpi eq, %217, %221 : i64
cf.cond_br %220, ^bb54, ^bb55
^bb54:
%223 = arith.constant 3 : i32
%224 = llvm.load %210 : !llvm.ptr -> i64
%226 = arith.extsi %223 : i32 to i64
%225 = arith.muli %226, %224 : i64
%227 = llvm.load %210 : !llvm.ptr -> i64
%228 = arith.muli %225, %227 : i64
%229 = arith.addi %228, %199 : i64
%222 = func.call @is_square(%229) : (i64) -> i64
%230 = arith.constant 0 : i32
%232 = arith.extsi %230 : i32 to i64
%231 = arith.cmpi sge, %222, %232 : i64
%233 = scf.if %231 -> (i1) {
%234 = arith.constant 1 : i32
%236 = arith.extsi %234 : i32 to i64
%235 = arith.andi %222, %236 : i64
%237 = arith.constant 0 : i32
%239 = arith.extsi %237 : i32 to i64
%238 = arith.cmpi eq, %235, %239 : i64
scf.yield %238 : i1
} else {
%240 = arith.constant false
scf.yield %240 : i1
}
cf.cond_br %233, ^bb57, ^bb58
^bb57:
%241 = arith.constant 0 : i32
%242 = arith.extsi %241 : i32 to i64
%243 = llvm.mlir.constant(1 : i64) : i64
%244 = llvm.alloca %243 x i64 : (i64) -> !llvm.ptr
llvm.store %242, %244 : i64, !llvm.ptr
%245 = arith.constant 0 : i32
%246 = arith.extsi %245 : i32 to i64
%247 = llvm.mlir.constant(1 : i64) : i64
%248 = llvm.alloca %247 x i64 : (i64) -> !llvm.ptr
llvm.store %246, %248 : i64, !llvm.ptr
cf.cond_br %205, ^bb60, ^bb61
^bb60:
%249 = arith.constant 2 : i32
%251 = arith.extsi %249 : i32 to i64
%250 = arith.muli %251, %222 : i64
%252 = arith.constant 3 : i32
%253 = llvm.load %210 : !llvm.ptr -> i64
%255 = arith.extsi %252 : i32 to i64
%254 = arith.muli %255, %253 : i64
%256 = arith.subi %250, %254 : i64
llvm.store %256, %244 : i64, !llvm.ptr
%257 = arith.constant 2 : i32
%258 = llvm.load %210 : !llvm.ptr -> i64
%260 = arith.extsi %257 : i32 to i64
%259 = arith.muli %260, %258 : i64
%261 = arith.subi %259, %222 : i64
llvm.store %261, %248 : i64, !llvm.ptr
cf.br ^bb62
^bb61:
%262 = arith.constant 7 : i32
%264 = arith.extsi %262 : i32 to i64
%263 = arith.muli %264, %222 : i64
%265 = arith.constant 12 : i32
%266 = llvm.load %210 : !llvm.ptr -> i64
%268 = arith.extsi %265 : i32 to i64
%267 = arith.muli %268, %266 : i64
%269 = arith.subi %263, %267 : i64
llvm.store %269, %244 : i64, !llvm.ptr
%270 = arith.constant 7 : i32
%271 = llvm.load %210 : !llvm.ptr -> i64
%273 = arith.extsi %270 : i32 to i64
%272 = arith.muli %273, %271 : i64
%274 = arith.constant 4 : i32
%276 = arith.extsi %274 : i32 to i64
%275 = arith.muli %276, %222 : i64
%277 = arith.subi %272, %275 : i64
llvm.store %277, %248 : i64, !llvm.ptr
cf.br ^bb62
^bb62:
%278 = arith.constant 1 : i1
%279 = llvm.mlir.constant(1 : i64) : i64
%280 = llvm.alloca %279 x i1 : (i64) -> !llvm.ptr
llvm.store %278, %280 : i1, !llvm.ptr
%281 = llvm.load %244 : !llvm.ptr -> i64
%282 = arith.constant 0 : i32
%284 = arith.extsi %282 : i32 to i64
%283 = arith.cmpi sgt, %281, %284 : i64
%285 = scf.if %283 -> (i1) {
%286 = llvm.load %248 : !llvm.ptr -> i64
%287 = arith.constant 0 : i32
%289 = arith.extsi %287 : i32 to i64
%288 = arith.cmpi sge, %286, %289 : i64
scf.yield %288 : i1
} else {
%290 = arith.constant false
scf.yield %290 : i1
}
%291 = scf.if %285 -> (i1) {
%292 = llvm.load %248 : !llvm.ptr -> i64
%293 = llvm.load %192 : !llvm.ptr -> i64
%294 = arith.subi %292, %293 : i64
%295 = arith.constant 1 : i32
%297 = arith.extsi %295 : i32 to i64
%296 = arith.andi %294, %297 : i64
%298 = arith.constant 0 : i32
%300 = arith.extsi %298 : i32 to i64
%299 = arith.cmpi eq, %296, %300 : i64
scf.yield %299 : i1
} else {
%301 = arith.constant false
scf.yield %301 : i1
}
%302 = scf.if %291 -> (i1) {
%303 = llvm.load %244 : !llvm.ptr -> i64
%304 = arith.constant 1 : i32
%306 = arith.extsi %304 : i32 to i64
%305 = arith.andi %303, %306 : i64
%307 = arith.constant 0 : i32
%309 = arith.extsi %307 : i32 to i64
%308 = arith.cmpi eq, %305, %309 : i64
scf.yield %308 : i1
} else {
%310 = arith.constant false
scf.yield %310 : i1
}
cf.cond_br %302, ^bb63, ^bb64
^bb63:
%311 = llvm.load %244 : !llvm.ptr -> i64
%312 = llvm.load %244 : !llvm.ptr -> i64
%313 = arith.muli %311, %312 : i64
%314 = arith.constant 3 : i32
%315 = llvm.load %248 : !llvm.ptr -> i64
%317 = arith.extsi %314 : i32 to i64
%316 = arith.muli %317, %315 : i64
%318 = llvm.load %248 : !llvm.ptr -> i64
%319 = arith.muli %316, %318 : i64
%320 = arith.subi %313, %319 : i64
%321 = arith.cmpi eq, %320, %199 : i64
cf.cond_br %321, ^bb66, ^bb67
^bb66:
%322 = arith.constant 0 : i1
llvm.store %322, %280 : i1, !llvm.ptr
cf.br ^bb68
^bb67:
cf.br ^bb68
^bb68:
cf.br ^bb65
^bb64:
cf.br ^bb65
^bb65:
%323 = llvm.load %280 : !llvm.ptr -> i1
cf.cond_br %323, ^bb69, ^bb70
^bb69:
%324 = llvm.load %186 : !llvm.ptr -> i64
%326 = llvm.load %210 : !llvm.ptr -> i64
%327 = llvm.load %192 : !llvm.ptr -> i64
%325 = func.call @count_orbit(%222, %326, %327, %205, %182) : (i64, i64, i64, i1, f64) -> i64
%328 = arith.addi %324, %325 : i64
llvm.store %328, %186 : i64, !llvm.ptr
cf.br ^bb71
^bb70:
cf.br ^bb71
^bb71:
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
cf.br ^bb56
^bb55:
cf.br ^bb56
^bb56:
%329 = llvm.load %210 : !llvm.ptr -> i64
%330 = arith.constant 1 : i32
%332 = arith.extsi %330 : i32 to i64
%331 = arith.addi %329, %332 : i64
llvm.store %331, %210 : i64, !llvm.ptr
cf.br ^bb51
^bb53:
%333 = llvm.load %192 : !llvm.ptr -> i64
%334 = arith.constant 1 : i32
%336 = arith.extsi %334 : i32 to i64
%335 = arith.addi %333, %336 : i64
llvm.store %335, %192 : i64, !llvm.ptr
cf.br ^bb48
^bb50:
%337 = llvm.load %186 : !llvm.ptr -> i64
func.return %337 : i64
}
func.func @main() -> i32 {
%338 = llvm.mlir.addressof @str_0 : !llvm.ptr
%339 = func.call @count_triangles() : () -> i64
%340 = llvm.call @printf(%338, %339) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%341 = arith.constant 0 : i32
func.return %341 : i32
}
}