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 611325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:46:18+00:00 2026-05-13T17:46:18+00:00

I tried to solve problem# 276 from Project Euler , but without success so

  • 0

I tried to solve problem# 276 from Project Euler, but without success so far.

Consider the triangles with integer
sides a, b and c with a ≤ b ≤ c. An
integer sided triangle (a,b,c) is
called primitive if gcd(a,b,c)=1. How
many primitive integer sided triangles
exist with a perimeter not exceeding
10 000 000?

The bottleneck in my code is the GCD function. It almost takes 90% of execution time for my sample input. I would love to hear hints, comments on how to improve the solution… My solution is written in C, but it is quit simple:

#include <stdio.h>
#include <math.h>

#define sides 3
// This is where my program spends most of its time
size_t bi_gcd (size_t a, size_t b);
size_t tri_gcd (size_t a, size_t b, size_t c);
size_t count_primitive_triangles (size_t maximum_perimeter);

int main()
{
 printf( "primitive_triangles = %lu \n",
            count_primitive_triangles(1000) );
}

size_t bi_gcd (size_t a, size_t b)
{
 size_t t;

 while(b != 0)
 {
  t = b;
  b = a % b;
  a = t;
 }

 return a;
}

size_t tri_gcd (size_t a, size_t b, size_t c)
{
 return bi_gcd(a, bi_gcd(b, c));
}

size_t count_primitive_triangles (size_t max_perimeter)
{
 size_t count = 0; // number of primitive triangles
 size_t a, b, c;   // sides of the triangle
 // the following are the bounds of each side
 size_t 
  // because b >= a && c >= b ==> max of a
  // is when a+b+c > 10,000,000
  // b == a (at least)
  // c == b (at least)
  // ==> a+b+c >= 10,000,000
  // ==> 3*a   >= 10,000,000
  // ==> a= 10,000,000/3
  a_limit = max_perimeter/sides,
  b_limit,
  c_limit;

 for (a = 1; a <= a_limit; ++a)
 {
  // because c >= b && a+b+c <= 10,000,000
  // ==> 2*b + a = 10,000,000
  // ==> 2*b = 10,000,000 - a
  // ==> b = (10,000,000 - a)/2
  for (b = a, b_limit = (max_perimeter-a)/2; b <= b_limit; ++b)
  {
   // The triangle inequality:
   // a+b > c (for a triangle to be valid!)
   // ==> c < a+b
   for (c = b, c_limit = a+b; c < c_limit; ++c)
   {
    if (tri_gcd(a, b, c) == 1)
     ++count;
   }
  }
 }

 return count;
}
  • 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-13T17:46:19+00:00Added an answer on May 13, 2026 at 5:46 pm

    It’s good that you’re profiling before optimizing, but the fact that a lot of time in the gcd function doesn’t necessarily mean you need to (or can) make it faster, rather that you’re calling it too often. 🙂
    Here’s a hint — an algorithmic improvement that will improve the running time by orders of magnitude, instead of merely an improvement in the implementation.

    You’re currently counting just the primitive triangles individually. Instead, ask yourself this: can you efficiently count all triangles (not necessarily primitive) with perimeter a+b+c=n? (Running time O(n) will do – your current algorithm is Ω(n3).) Having done that, which triangles have you over-counted? For example, how many triangles are there with p dividing the sides? (Hint: a+b+c=n <=> (a/p) + (b/p) + (c/p) = n/p.) And so on.

    Edit: After solving the problem and checking the thread on Project Euler, I found there are some other nice approaches to this problem, but the above approach is most common, and it works. For the first part, you can count it directly (some people have done it; it’s certainly doable), or you may find this additional hint/trick helpful:

    • Suppose 1 ≤ a ≤ b ≤ c, and a+b+c = n.
      Let p = b+c-a = n-2a, let q = c+a-b = n-2b, and let r = a+b-c = n-2c.
      Then 1 ≤ r ≤ q ≤ p, and p+q+r = a+b+c = n.
      Also, p,q,r all have the same parity as n.
    • Conversely, for any 1 ≤ r ≤ q ≤ p with p+q+r = n with all three having the same parity (as n),
      let a = (q+r)/2, let b = (r+p)/2, c = (p+q)/2.
      Then 1 ≤ a ≤ b ≤ c and a+b+c=n.
      Further, these are integers and form a triangle (check).
    • So the number of integer triangles (a,b,c) with perimeter n
      is exactly
      the number of partitions of n into parts (p,q,r) of same parity. You may find this easier to count.

    Additionally/alternatively, try directly relating this number T(n) to smaller numbers T(n-k), to get a recurrence relation.

    (Of course, there are also some very simple formulae you can find if you Google, but what’s the fun in that?)

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer isn't it a case of the dreaded early optimisation? "Hundreds… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer What do you want the resolution for? If you are… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer You can tell by looking at HTTP Response Headers -… May 15, 2026 at 5:25 pm

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.