Skip to content

Commit

Permalink
leetcode: Add largest lcm
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 24, 2024
1 parent 1bdfd56 commit 78a5ff8
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
21 changes: 21 additions & 0 deletions math/src/primes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#include <cassert>

#include <vector>

std::vector<int> get_primes(int n) {
assert(n > 0);
std::vector<bool> primes(true, n + 1);
for (int i = 2; i < n + 1; ++i) {
if (!primes[i]) {
continue;
}
for (int j = i + 1; j < n + 1; ++j) {
if (j % i == 0) {
primes[j] = false;
}
}
}

return {};
}
7 changes: 7 additions & 0 deletions src/leetcode/0000.largest_lcm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0000-largest-lcm"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
3 changes: 3 additions & 0 deletions src/leetcode/0000.largest_lcm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

build: main.cpp
g++ -g main.cpp -o main
12 changes: 12 additions & 0 deletions src/leetcode/0000.largest_lcm/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# 求能整除1-n的最小正整数

```text
输入一个n (1 <= n <= 10000),
求能整除1-n的最小正整数,即最小公倍数.
由于数可能比较大,输出结果 mod 987654321.
例子:
输入: 3
输出: 6 (6是能整除1, 2, 3的最小正整数)
```
Binary file added src/leetcode/0000.largest_lcm/main
Binary file not shown.
81 changes: 81 additions & 0 deletions src/leetcode/0000.largest_lcm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

#include <cassert>

#include <iostream>
#include <vector>

int solution(int n) {
// 解题思路:
// 1. 先计算 1..n 之间的所有质数
// 2. 创建一个数组, 用于存放每个质因数的最大出现次数
// 3. 遍历 1..n 之间所有整数, 进行因式分解, 然后更新最大质因数数组
// 4. 将最大质因数数组中的值相乘, 即可得到结果.

// 得到质数
const int max_num = n + 1;
std::vector<bool> primes(max_num, true);
for (int i = 2; i <= n; ++i) {
if (!primes[i]) {
continue;
}
for (int j = i + 1; j <= n; ++j) {
if (j % i == 0) {
primes[j] = false;
}
}
}

std::vector<int> max_factors(max_num, 0);
// 遍历所有整数
for (int num = 2; num <= n; ++num) {
// 遍历所有质数
int rem = num;
for (int j = 2; j <= num; ++j) {
if (!primes[j]) {
continue;
}

// 确定当前质数作为质因数的次数.
int factor_count = 0;
while (rem % j == 0) {
rem /= j;
factor_count += 1;
}

// 更新质因数的最大个数
if (factor_count > max_factors[j]) {
max_factors[j] = factor_count;
}

// 当前整数 num 已经被除尽了
if (rem == 0) {
break;
}
}
}

// 最后, 计算最大质因数的积.
int ans = 1;
const int kModular = 987654321;
for (int num = 2; num <= n; ++num) {
if (max_factors[num] > 0) {
for (int i = 0; i < max_factors[num]; ++i) {
ans = ans * num % kModular;
}
}
}

return ans;
}

void check_solution() {
assert(solution(3) == 6);
}

int main() {
check_solution();
return 0;
}
27 changes: 27 additions & 0 deletions src/leetcode/0000.largest_lcm/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

pub fn solution1() {
todo!();
}

pub type SolutionFn = fn();

fn check_solution(_func: SolutionFn) {
todo!();
}

fn main() {
check_solution(solution1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, solution1};

#[test]
fn test_solution1() {
check_solution(solution1);
}
}

0 comments on commit 78a5ff8

Please sign in to comment.