Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:13+00:00 2026-05-26T20:44:13+00:00

before I start I want to clarify that I am not looking for code

  • 0

before I start I want to clarify that I am not looking for code examples to get the answer; that would defeat the object of Project Euler.

The problem can be found here http://projecteuler.net/problem=3

I think I have a way of solving the problem, but the Algorithm is VERY slow; it has been running for nearly two and a half hours now. So I am looking for general advice on optimisation.

Thanks.

#include<iostream>
using namespace std;

bool primality(int);

int main(){
  long long lim =  600851475143;
  long long div = lim/2;
  bool run = true;

  while(run){
    if(lim%div==0 && primality(div)){
      cout << "HPF: " << div;
      run = false;
    }
    else{
      div--;
    }
    if(div<=1){
      break;
    }
  }

  return 0;
}

bool primality(int num){
  for(int i=2; i<num; i++){
    if(num%i==0 && i!=num){
      return false;
    }
    else{
      return true;
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:44:14+00:00Added an answer on May 26, 2026 at 8:44 pm

    If you start div at 2 and count up instead of down, and divide it out from the number when the modulo is zero, you gain two big advantages that are useful here:

    1. You don’t have to check if div is prime, since it can’t be composite because any prime factors smaller than it would already have been divided out.
    2. You reduce the remaining problem size every time you find a factor, and, as it turns out, the input number has fairly small prime factors.

    You could then also break once div*div is greater than the remaining number, as you know at that point that it must be a prime. This is because any divisors greater than the square root are “paired” with one less than the square root. However, since this is an “easy” problem, this optimization is not needed here (although it is useful for later problems).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a windows service that would start before user can login.
I want to get one record before start date and end date DtpFrom means
I want to start looking at WCF but to be honest before I do
Before you start firing at me, I'm NOT looking to do this, but someone
Before I start, I want to point out that I'm pretty sure this actually
Before you start reading I would like to clarify: I have already thought of
I have an assignment and i need working code. Before i start i want
Before I start using CORBA I want to know something. It would seem intuitive
Before I start, yes this is a homework. I would not have posted here
I usually try to do TDD with not much analysis (no diagrams) before start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.