Project Euler Solution: Problem 3

Largest prime factor

Problem 3

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

What is the largest prime factor of the number 600851475143 ?

source link: https://projecteuler.net/problem=3

The time comlexity of my last solution is too large.

I think the shink the size of iteration condition number will optimize that.

Here is my original code:

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <cmath>
 5 #include <iostream>
 6
 7 using namespace std;
 8
 9 int main()
10 {
11     long long int const size = 600851475143;
12     vector<int> pr;
13     pr.push_back(2);
14     int maxPr = 2;
15     for (long int i = 3; i < sqrtl(size); i++) {
16         bool isPr = true;
17         for (long int cnt = 1; pr[cnt] < sqrtl(pr.back()); cnt++) {
18             if (i % pr[cnt] == 0) {
19                 isPr = false;
20                 break;
21             }
22         }
23
24         if (isPr) {
25             pr.push_back(i);
26             if (size % i == 0) {
27                 maxPr = i;
28                 cout << maxPr << endl;
29             }
30
31         }
32
33         return 0;
34     }
35 }
时间: 2024-10-22 22:22:50

Project Euler Solution: Problem 3的相关文章

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler: Solution for Problem 5

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? Source link: http

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 67 Maximum path sum II

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (righ

Project Euler:Problem 68 Magic 5-gon ring

Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine. Working clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9

Project Euler:Problem 78 Coin partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7. OOOOO OOOO   O OOO   OO OOO   O   O OO   OO   O OO   O   O