Thread: project Euler

  1. #16
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I didn't realize we were going beyond simple brute force.
    20 minutes of coding later calculates up to the 4480 factor value in less than 2 seconds (on a slow machine).
    This takes less than 45 seconds.
    Code:
      Limit    Index          Number Factors
    >= 1000    41040       842161320    1024
    >= 2000   313599     49172323200    2304
    >= 3000   453375    102774672000    3072
    >= 4000  1890944   1787835551040    4480
    >= 5000  2203200   2427046221600    5760
    >= 6000  2756159   3798207594720    6144
    >= 7000  7289919  26571463158240    7680
    >= 8000 10497024  55093761676800    8640
    >= 9000 11851839  70233049766880    9216
    >=10000 14753024 108825865948800   13824
    But I guess code is secret now.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  2. #17
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by john.c View Post
    But I guess code is secret now.
    Still brute force, and the code isn't secret, just not that well written or interesting. Here's the most interesting bit:

    Code:
    struct Factor {
       uint64_t factor;
       uint64_t power;
    };
    
    
    struct Factor_List {
       uint32_t used;
       struct Factor factors[64];  // Any more than 64 unique factors means the number is way more than 2^64
    
    };
    
    
    int main(int argc, char *argv[])
    {
       find_primes(1000000000);
       struct Factor_List fl1,fl2;
       int limit = 125;
       uint64_t a = 2;
       uint64_t b = 3;
       find_factors(a/2, &fl1);  // Remove the factor of 2 from 'a'
       while(a < 1000000000) {
          int factors;
          find_factors(b, &fl2);
          factors = count_factors(&fl1,&fl2);
          if(factors > limit) {
              printf("The %ldth triangular number %ld has %d factors\n", a, a*(a+1)/2, factors);
              while(limit < factors)
                 limit *= 2;
          }
          a+= 2;
    
    
          find_factors(a/2, &fl1);  // Remove the factor of 2 from 'a'
          factors = count_factors(&fl1,&fl2);
          if(factors > limit) {
              printf("The %ldth triangular number %ld has %d factors\n", b, b*(b+1)/2, factors);
              while(limit < factors)
                 limit *= 2;
          }
          b+= 2;
       }
    }
    Last edited by hamster_nz; 05-20-2023 at 06:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help!! Problem 3 of Project Euler
    By i++ in forum C Programming
    Replies: 8
    Last Post: 08-15-2013, 06:59 PM
  2. Project Euler 22
    By deadrabbit in forum C Programming
    Replies: 1
    Last Post: 06-23-2012, 11:22 PM
  3. Project Euler problem
    By nimitzhunter in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2012, 09:00 PM
  4. Project Euler
    By CodeGuru25 in forum C Programming
    Replies: 2
    Last Post: 01-13-2010, 06:25 AM
  5. Project Euler Question
    By Head In Jar Man in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2009, 02:59 PM

Tags for this Thread