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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:37+00:00 2026-05-25T23:04:37+00:00

This code takes an extremely long time to run (more than 10 minutes). Is

  • 0

This code takes an extremely long time to run (more than 10 minutes). Is there any way in which I can optimize it so that it finishes in less than one minute?

clear all;
for i = 1:1000000
    harmonicsum = 0;
    lhs = 0;
    for j = 1:i
        % compute harmonic sum
        harmonicsum = harmonicsum + 1/j;
        % find sum of factors
        if (mod(i,j)==0)
            lhs = lhs + j;
        end
    end
    %define right hand side (rhs) of Riemann Hypothesis
    rhs = harmonicsum + log(harmonicsum) * exp(harmonicsum);

    if lhs > rhs
        disp('Hypothesis violated')
    end
end
  • 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-25T23:04:37+00:00Added an answer on May 25, 2026 at 11:04 pm

    @b3 has a great vectorization of rhs.

    One typo though, needs to use times and not mtimes:

    harmonicsum = cumsum(1 ./ (1:1e6));
    rhs = harmonicsum + log(harmonicsum) .* exp(harmonicsum);
    

    For lhs, I propose the following, loosely based on Eratosthenes’ Sieve:

    lhs = 1 + [1:1e6];
    lhs(1) = 1;
    for iii = 2:numel(lhs)/2
        lhs(2*iii:iii:end) = lhs(2*iii:iii:end) + iii;
    end;
    

    Execution time is just 2.45 seconds (for this half of the problem). Total including calculation of rhs and find is under 3 seconds.

    I’m currently running the other version to make sure that the results are equal.


    EDIT: found a bug with lhs(1) and special-cased it (it is a special case, the only natural number where 1 and N aren’t distinct factors)

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

Sidebar

Related Questions

Problem Hello all! I have this code which takes my jpg image loops through
In my computer this code takes 17 seconds (1000 millions times): static void Main(string[]
So I have this code that takes care of command acknowledgment from remote computers,
i found this code at activestate, it takes a string and prints permutations of
I use this code which is taken from MVC futures and attach the Attribute
I have this extremely strange behavior coming : In the below code: If I
Summary: How can I reduce the amount of time it takes to convert tifs
I'm trying to simplify some code where I have an extremely long switch statement
Why does this code 500.times { Time.now.in_time_zone('Helsinki') } take several seconds with Rails 3.1
As you all know, it's extremely common to have code like this: <a href='#'

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.