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

  • Home
  • SEARCH
  • 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 194001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:33:29+00:00 2026-05-11T16:33:29+00:00

I am becoming more and more addicted to the Project Euler problems. However since

  • 0

I am becoming more and more addicted to the Project Euler problems. However since one week I am stuck with the #214.

Here is a short version of the problem:
PHI() is Euler’s totient function, i.e. for any given integer n, PHI(n)=numbers of k<=n for which gcd(k,n)=1.

We can iterate PHI() to create a chain. For example starting from 18:

PHI(18)=6 => PHI(6)=2 => PHI(2)=1.

So starting from 18 we get a chain of length 4 (18,6,2,1)

The problem is to calculate the sum of all primes less than 40e6 which generate a chain of length 25.

I built a function that calculates the chain length of any number and I tested it for
small values: it works well and fast.

sum of all primes<=20 which generate a chain of length 4: 12
sum of all primes<=1000 which generate a chain of length 10: 39383

Unfortunately my algorithm doesn’t scale well. When I apply it to the problem, it takes several hours to calculate… so I stop it because the Project Euler problems must be solved in less than one minute.

I thought that my prime detection function might be slow so I fed the program with a list of primes <40e6 to avoid the primality test… The code runs now a little bit faster, but there is still no way to get a solution in less than a few hours (and I don’t want this).

So is there any “magic trick” that I am missing here ? I really don’t understand how to be more efficient on this one…

I am not asking for the solution, because fighting with optimization is all the fun of Project Euler. However, any small hint that could put me on the right track would be welcome.

Thanks !


Sorry the formatting of the comment is wrong and I can’t delete it. So here it is again:

To calculate the totient function, I use the following:
For a given n, let’s call p the list of it’s factors.
phi(n)=n*prod((p-1)/p)

ex: 2,3 are factors of 18 => phi(18) = 18 * 1/2 * 2/3 = 6

So in this way I don’t calculate any gcd. The function that gives me the factors is built in MATLAB (yep, I forgot to mention that I code in Matlab). Maybe I should rewrite the factorization function…

  • 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-11T16:33:29+00:00Added an answer on May 11, 2026 at 4:33 pm

    I’ll make a wild guess that the time-consuming part is calculating the totients of the numbers.

    One thought could be to try and generate these in some clever way. Think about if it would be possible to calculate them all at once, instead of doing the calculation for one number at a time…

    Also, how are you calculating the totient function? The definition (number of integers k where gcd(n,k)==1) is not a useful way to work with it. Look it up and see if you can find a more suitable formula for it.

    Edit:
    Yup, that’s the expression I was after. But going through each integer, factoring it, and computing the totient is too slow. Look for a way to compute the totients without doing any factoring…

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

Sidebar

Ask A Question

Stats

  • Questions 132k
  • Answers 132k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I assume you're using the stock WPF validation mechanism -… May 12, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer A lot of distributed systems built on DDD are using… May 12, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer I alway prefer to use the NHibernate Event system to… May 12, 2026 at 6:30 am

Related Questions

I have a PHP application where I would like to certain objects to persist
I have a legacy VB6 ActiveX control used in IE to provide control of
I am reading log files but not all lines want to be processed straight
I've always admired the original uTorrent program. It looked great, was less than 64kb,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.