欧拉计划第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最大的质因数是多少?

解题思路

分解质因数的算法是 \(O( \sqrt{n} )\) 的算法。

求一个数 \(a\) 的质因数,可以从 \(2\) 开始到 \(\sqrt{n}\) 去枚举每一个数 \(i\),然后只要 \(a\) 能够被 \(i\) 整除则一直除 \(i\)。
循环结束的时候如果 \(a\) 没有被除尽(即:\(a\) 不为 \(1\)),则这个没有被除尽的部分就是最后一个 \(a\) 的质因数。

我们可以按照这种方案求解 \(600851475143\) 的最大的那个质因数。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long cal(long long a) {
    long long res, b = sqrt(a);
    for (long long i = 2; i <= b; i ++) {
        if (a % i == 0) {
            res = i;
            while (a % i == 0) a /= i;
        }
    }
    if (a != 1) res = a;
    return res;
}
int main() {
    cout << cal(600851475143LL) << endl;
    return 0;
}

得到答案为 \(6857\)。

原文地址:https://www.cnblogs.com/quanjun/p/12322620.html

时间: 2024-08-26 08:29:02

欧拉计划第3题题解的相关文章

欧拉计划第10题题解

Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 素数的和 所有小于10的素数的和是2 + 3 + 5 + 7 = 17. 求所有小于两百万的素数的和. 解题思路 没有特别好的想法,下奶能想到的就是枚举算出200万以内的所有素数,然后求这些素数的和. 实现代码如下: #include <bits/st

欧拉计划第8题题解

Largest product in a series The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 8586156078911

欧拉计划第9题题解

Special Pythagorean triplet A Pythagorean triplet is a set of three natural numbers, \(a < b < c\) , for which, \(a^2 + b^2 = c^2\) For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\) . There exists exactly one Pythagorean triplet for which \(a + b + c =

欧拉计划第13题题解

Large sum Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629

欧拉计划&#183;第十一题

题目11:在20×20的网格中同一直线上四个数的最大乘积是多少? 在以下这个20*20的网格中,四个处于同一对角线上的相邻数字用红色标了出来: 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 6

欧拉计划&#183;第十题

题目10:计算两百万以下所有质数的和. 10以下的质数的和是2 + 3 + 5 + 7 = 17. 找出两百万以下所有质数的和. 源码 STDMETHODIMP COuLa::Test10(int number) { // TODO: 在此添加实现代码 __int64 sum = 2; for(int i = 2; 2*i-1 <= number; i++) { int c = 2*i-1; for( int j = 2;c>10? j<=10 : j<=c;j++) { if(1

欧拉计划&#183;第七题

题目7:找出第10001个质数. 前六个质数是2,3,5,7,11和13,其中第6个是13. 第10001个质数是多少? 源码 STDMETHODIMP COuLa::Test7(int number) { // TODO: 在此添加实现代码 int iNumberForCout = 1; int iNumberForOutput = 0; int iNumberForAdd = 1; while(iNumberForCout<number) { iNumberForAdd++; iNumber

欧拉计划&#183;第八题

题目8:找出这个1000位数字中连续13个数字乘积的最大值. 找出以下这个1000位的整数中连续13个数字的最大乘积. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 1254069874715852386305071569329096329

欧拉计划&#183;第五题

题目5:找出最小的能被1-20中每个数整除的数. 2520是最小的能被1-10中每个数字整除的正整数. 最小的能被1-20中每个数整除的正整数是多少? 源码 STDMETHODIMP COuLa::Test5(int number) { // TODO: 在此添加实现代码 int iForNumber[MAX_PATH] = {0}; int iForFinalNumber[MAX_PATH] = {0}; for(int i = number; i>0; i--) { int c = i; i