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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:27:11+00:00 2026-06-13T14:27:11+00:00

Context: I’m working on Project Euler Problem 23 using Matlab in order to practice

  • 0

Context: I’m working on Project Euler Problem 23 using Matlab in order to practice my barely existing programming skills.

My Problem:

Now I have a vector with roughly 6500 numbers (ranging from 12 to 28122) as elements and want to calculate all the two element sums. That is I only need one instance of every sum, so having calculated a1 + an it’s not necessary to calculate an + a1.

Edit for clarification: This includes the sums a1+a1, a2+a2,…, an+an.

The problem is that this is much too slow.

Problem specific constraints:

It’s a given that sums 28123 or over aren’t necessary to calculate, since those can’t be used to solve the problem further.

My approach:

AbundentNumberSumsRaw=[];
for i=1:3490
    AbundentNumberSumsRaw=[AbundentNumberSumRaw AbundentNumbers(i)+AbundentNumbers(i:end);
end

This works terribly :p

My Comments:

I’m pretty sure that incrementally increasing the vector AbundentNumbersRaw is bad coding, since that means memory usage will spike unnecessarily. I haven’t done so, since a) I don’t know what size vector to pre-allocate and b) I couldn’t come up with a way to inject the sums into AbundentNumbersRaw in a orderly manner without using some ugly looking nested loops.

“for i=1:3490” is lower than the numbers of elements simply because I checked and saw that all the resulting sums for numbers whose index are above 3490 would be too large for me to use anyway.

I’m pretty sure my main issue is that the program need to do a lot of incremental increases of the vector AbundentNumbersRaw.

Any and all help and suggestions would be much appreciated 🙂

Cheers

Rasmus

  • 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-06-13T14:27:12+00:00Added an answer on June 13, 2026 at 2:27 pm

    Suppose

    a = 28110*rand(6500,1)+12;
    

    then

    sums = [
     a(1) + a(1:end)
     a(2) + a(2:end)
     ... 
    ];
    

    is the calculation you’re after.

    You also state that sums whose value goes over 28123 should be discarded.

    This can be generalized like so:

    % Compute all 2-element sums without repetitions
    C = arrayfun(@(x) a(x)+a(x:end), 1:numel(a), 'uniformoutput', false);
    C = cat(1, C{:});
    
    % discard sums exceeding threshold
    C(C>28123) = [];
    

    or using a loop

    % Compute all 2-element sums without repetitions
    E = cell(numel(a),1);
    for ii = 1:numel(a)
        E{ii} = a(ii)+a(ii:end); end
    E = cat(1, E{:});
    
    % discard sums exceeding threshold
    E(E>28123) = [];
    

    Simple testing shows that arrayfun is somewhat faster than the loop, so I’d go for the arrayfun option.

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

Sidebar

Related Questions

Context: I have been working on and off on an application which uses a
Context : python 2.6.5 environment I am using unittest.defaultTestLoader.loadTestsFromModule(module) to load tests. However, when
Context: I'm working on exercises in Software Foundations . Theorem neg_move : forall x
Context I'm working on a small web app to store photos. Photos are ordered
Context : programming a c/c++ win32-mfc library How to know whether we are in
Context: I was going to build app using mod_rewrite (front page loading child pages),
Context We have this application, using about 60 coding projects. We have several products
Context I'm learning C, and I'm trying to reverse a string in place using
Context The case is screen scraping web content using QuotaXML SDK 1.6 to finally
Context: asp.net MVC 3 app. Page1 is a mobile page using jquery mobile and

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.