数的分解、分解质因数

62 篇文章 5 订阅
  • 21 ⇒ 6 + 7 + 8(环形嵌套,内层半径较小) = 7 + 7 + 7,只要是能被 3 整除的数,
    • 24 ⇒ 7 + 8 + 9 = 8 + 8 + 8

1. 实现之一

def primes(n):
    x, l = 2, []
    while True:
        if x > n:
            break
        if n % x == 0:
            n //= x
            l.append(x)
            x -= 1
        x += 1
    return l
>> primes(7854)
[2, 3, 7, 11, 17]

注:有待优化;

2. 方法之二

vector<int> primes(int n){
    if (n == 1) return vector<int>(1, 1);
    vector<int> ret;
    for (int div=2; n>1; ++div){
        while (n % div == 0){
            n /= div;
            ret.push_back(n);
        }
    }
    return ret;
}

二者其实在本质上是一致的;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
因数分解器v1.1采用C语言编写,可以分解0~2^64-1的,速度很快。 部分字的分解结果: ************************************************** * Factorization v1.1 * DevilHand Presents 2011-03-18 * Email: DevilHand@126.com ************************************************** Input a number: 12157665459056928801 Result: 12157665459056928801 = 3^40 ************************************************** Input a number: 9966332255887744111 Result: 9966332255887744111 is a prime number. ************************************************** Input a number: 18446744073709551520 Result: 18446744073709551520 = 2^5 x 5 x 2663 x 43294085790719 ************************************************** Input a number: 18446744073709551608 Result: 18446744073709551608 = 2^3 x 2305843009213693951 ************************************************** Input a number: 10459 Result: 10459 is a prime number. ************************************************** Input a number: 90541 Result: 90541 = 11 x 8231 ************************************************** Input a number: 142857 Result: 142857 = 3^3 x 11 x 13 x 37 ************************************************** Input a number: 428571 Result: 428571 = 3^4 x 11 x 13 x 37 ************************************************** Input a number: 999999 Result: 999999 = 3^3 x 7 x 11 x 13 x 37 ************************************************** Input a number: 951463287 Result: 951463287 = 3^3 x 23 x 547 x 2801 ************************************************** Input a number: 614889782588491410 Result: 614889782588491410 = 2 x 3 x 5 x 7 x 11 x 13 x 17 x 19 x 23 x 29 x 31 x 37 x 41 x 43 x 47 ************************************************** Input a number: 142857142857 Result: 142857142857 = 3^3 x 11 x 13 x 37 x 101 x 9901 ************************************************** Input a number: 114422885577 Result: 114422885577 = 3^4 x 7 x 11 x 13 x 37 x 43 x 887 ************************************************** Input a number: 9080706050403020100 Result: 9080706050403020100 = 2^2 x 3^2 x 5^2 x 229 x 547 x 8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值