REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
factorize.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include "utils/factorize.h"
7
8#include <Arduino.h> // FLASHMEM
9#include <math.h>
10
11FLASHMEM
12std::pair<unsigned long long, unsigned long long> utils::factorize(unsigned long long input,
13 unsigned int acceptable_delta) {
14 unsigned long long root = sqrt(input);
15 if (root * root > input - acceptable_delta)
16 return std::make_pair(root, root);
17
18 unsigned long long x = root, y = root;
19
20begin:
21 if (x * y > input + acceptable_delta) {
22 y--;
23 x = input / y;
24 goto begin;
25 } else if (x * y < input - acceptable_delta) {
26 x++;
27 y = input / x;
28 goto begin;
29 } else
30 return std::make_pair(x, y);
31}