Problem 3 : Largest prime factor

原创 2018年04月17日 21:50:43

Problem 3


Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


最大质因数

13195的所有质因数为5、7、13和29。

600851475143最大的质因数是多少?


题目解答

    朴素解法:暴力枚举(枚举所有因子并判断是否为素数即可)

       优化算法:

            在这里我们可以利用 算术基本定理 来解决这个问题。

            算术基本定理:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积。

               遇到的素数不断分解即可,直到分解到1时退出,我们的数字是从小到大遍历的,所以最后分解到一时的素因子一定时最大的。

  题目代码

#include <stdio.h>
#include <inttypes.h>
#define N 600851475143
int main(){
    int32_t ans;
    int64_t temp = N;
    for (int32_t i=2;;i++){
        if (temp % i == 0){
            while (temp % i == 0){
                temp /= i;
            }
            ans = i;
            if(temp == 1){
                break;
            }
        }
    }
    printf("%d\n",ans);
}

                

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ruger008/article/details/79981148

Problem 3:Largest prime factor

原文地址:http://projecteuler.net/problem=3 Largest prime factor Problem 3 The prime factors of 131...
  • cfeibiao
  • cfeibiao
  • 2013-08-01 23:42:19
  • 777

Problem 3 Largest prime factor (分解素因子)

Largest prime factor Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is th...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2016-10-25 21:49:00
  • 217

Largest prime factor(最大素数因子)

Largest prime factor   Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java...
  • qaz135135135
  • qaz135135135
  • 2016-07-21 09:42:06
  • 707

(hdu step 2.1.3)Largest prime factor(求一个数的最大质因子的位置)

题目:       Largest prime factorTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja...
  • caihongshijie6
  • caihongshijie6
  • 2015-01-30 20:34:50
  • 2089

Project Euler -- 3 Largest prime factor

Project Euler Largest prime factor
  • alzc2012
  • alzc2012
  • 2015-05-09 11:34:27
  • 272

杭电OJ 2136 Largest prime factor

Largest primefactor Problem Description Everybody knowsany number can be combined by the prime num...
  • wyf12138
  • wyf12138
  • 2016-06-16 20:01:20
  • 381

欧拉计划---0003 Largest prime factor(找出一个合数的最大质数因子)

第二题原文如下: Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the larg...
  • zd199218
  • zd199218
  • 2015-02-07 17:21:21
  • 782

HDOJ(HDU) 2136 Largest prime factor(素数筛选)

Problem Description Everybody knows any number can be combined by the prime number. Now, your task...
  • qq_26525215
  • qq_26525215
  • 2016-05-03 20:30:42
  • 10873

HDU 2136 Largest prime factor (水)

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • ACResume
  • ACResume
  • 2012-06-19 08:09:00
  • 2981

projecteuler网站第三题解法:Get the largest prime factor of a number

原题酱紫的:The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number ...
  • YouthWang
  • YouthWang
  • 2010-05-23 14:18:00
  • 800
收藏助手
不良信息举报
您举报文章:Problem 3 : Largest prime factor
举报原因:
原因补充:

(最多只允许输入30个字)