【Project Euler】【Problem 5】Smallest multiple

本文探讨了如何找出能被1至20所有数字整除的最小正数,并提供了一段C语言代码实现,该算法通过分解每个数字的质因数并合并不同质因数来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Smallest multiple

Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?


答案:
232792560

最小倍数

Problem 5

2520是最小的可以被1到10整除的数

哪个正数可以被1到20整除?


答案:
232792560
#include <stdio.h>
#include <math.h>

//将1-20之间的数全部进行分解因子,剔除相同因子,然后合并。

int main(int argc, char *argv[])
{
    int result = 1;

    int primes[5];
    int primes_length = -1;

    int quot = 0;
    for (int i=2;i<=20;i++)
    {
        primes_length = 0;
        quot = i;
        for (int j = 2; j <= i; j++)
        {
            if (quot % j ==0)
            {
                quot = quot / j;
                primes[primes_length++] = j;
                j = 1;
            }
        }

        for (int j = 0;j<primes_length ;j++ )
        {
            if (result % primes[j] == 0)
            {
                result /= primes[j];
            }
        }
        for (int j = 0;j<primes_length ;j++ )
        {
            result *= primes[j];
        }
    }

    printf("result is : %d",result);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值