add gcd and prime factors

This commit is contained in:
2025-03-07 09:51:53 +03:00
commit 064a2db2ee
10 changed files with 206 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
build
.cache
subprojects/*
!subprojects/*.wrap

1
README.md Normal file
View File

@@ -0,0 +1 @@
Рабочий проект по изучению и анализу всяких там умных и не очень алгоритмов.

19
bin/main.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <hack/logger/logger.hpp>
#include "numbers/gcd.hpp"
#include "numbers/prime_factors.hpp"
auto main() -> int
{
hack::log()(algorithms::gcd(4851, 3003));
hack::log()(algorithms::gcd(64, 28));
hack::log()(algorithms::prime_factors_v1(127));
hack::log()(algorithms::prime_factors_v1(128));
hack::log()(algorithms::prime_factors_v1(130));
hack::log()(algorithms::prime_factors_v2(127));
hack::log()(algorithms::prime_factors_v2(128));
hack::log()(algorithms::prime_factors_v2(130));
}

8
bin/meson.build Normal file
View File

@@ -0,0 +1,8 @@
executable(
meson.project_name(),
'main.cpp',
dependencies : deps,
cpp_args: args,
include_directories : inc
)

37
meson.build Normal file
View File

@@ -0,0 +1,37 @@
project(
meson.current_source_dir().split('/').get(-1),
'cpp',
version : run_command('git', 'rev-parse', '--short', 'HEAD', check: false).stdout().strip(),
default_options : [
'warning_level=1',
'optimization=3',
'cpp_std=c++20',
])
add_project_arguments (
'-Wpedantic',
'-Wno-shadow',
'-Wno-unused-but-set-variable',
'-Wno-comment',
'-Wno-unused-parameter',
'-Wno-unused-value',
'-Wno-missing-field-initializers',
'-Wno-narrowing',
'-Wno-deprecated-enum-enum-conversion',
'-Wno-volatile',
'-Wno-format-security',
'-Wno-switch',
'-Wno-ignored-attributes',
'-Wno-unused-variable',
'-Wno-deprecated-enum-enum-conversion',
language: 'cpp'
)
args = []
inc = []
deps = [
subproject('hack').get_variable('hack_dep')
]
subdir('src')
subdir('bin')

29
run.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/bin/zsh
PROJECT_NAME=$(basename $PWD)
run() {
command meson compile -C build
cd build
./bin/$PROJECT_NAME
cd ..
}
# run test [name_test]
# example: run test pattrens
if [[ "$1" == "test" ]]; then
echo ""
meson test $2 -C build
echo ""
awk '/^-------------------------------------------------------------------------------/{flag=1} /===============================================================================/{flag=0} flag' ./build/meson-logs/testlog.txt
elif [[ "$1" == "tests" ]]; then
echo ""
meson test -C build
echo ""
# awk '/^-------------------------------------------------------------------------------/{flag=1} /===============================================================================/{flag=0} flag' ./build/meson-logs/testlog.txt
elif [[ -d "build" ]]; then
run
else
command meson setup build
run
fi

24
src/meson.build Normal file
View File

@@ -0,0 +1,24 @@
inc += include_directories('.')
headers = [
'numbers/gcd.hpp',
'numbers/prime_factors.hpp'
]
sources = [
]
lib = library(
meson.project_name(),
include_directories : inc,
sources: [headers, sources],
dependencies : deps,
cpp_args: args
)
dsp_sdk_dep = declare_dependency(
include_directories: inc,
link_with: lib
)
deps += dsp_sdk_dep

18
src/numbers/gcd.hpp Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
// Называется алгоритм Евклида
// Находит наибольший общий делитель
namespace algorithms
{
inline int gcd(int a, int b)
{
while (b != 0)
{
int r = a%b;
a = b;
b = r;
}
return a;
}
}

View File

@@ -0,0 +1,59 @@
#pragma once
#include <vector>
#include <math.h>
// Находит все простые множители заданного числа.
// Простое число - это число > 1, которое делится на 1 и на само себя.
namespace algorithms
{
inline std::vector<int> prime_factors_v1(int a)
{
std::vector<int> result;
int i = 2;
while (i < a)
{
while (a%i == 0)
{
result.push_back(i);
a /= i;
}
++i;
}
if (a > 1) result.push_back(a);
return result;
}
inline std::vector<int> prime_factors_v2(int a)
{
std::vector<int> result;
while(a%2 == 0)
{
result.push_back(2);
a = a/2;
}
int i = 3;
int max_faxtor = std::sqrt(a);
while (i <= max_faxtor)
{
while (a%i == 0)
{
result.push_back(i);
a /= i;
max_faxtor = std::sqrt(a);
}
i += 2;
}
if (a > 1) result.push_back(a);
return result;
}
}

7
subprojects/hack.wrap Normal file
View File

@@ -0,0 +1,7 @@
[wrap-git]
url = https://gitcast.ru/chatlanin/hack.git
revision = master
[provide]
hack = hack_dep