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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:41:39+00:00 2026-05-27T07:41:39+00:00

Why is not the same answer for following? Normal coding: long sum = 0;

  • 0

Why is not the same answer for following?

Normal coding:

long sum = 0;

for (long i = 1; i <= 10; i++)
{
    long result = 1;

    for (long j = 1; j <= i; j++)
    {
        result = result*j;
    }
    sum = sum + result;
}

Parallel Coding:

long sum = 0;

Parallel.For(1, 10, delegate(int i)
    {
        long result = 1;

        Parallel.For(1, i, delegate(int j)
            {
                result = result*j;
            });

        sum = sum + result;
    });

Please show me the right way

for (long i = 1; i <= 5; i++)
        {
            sum = sum * i;
        }

and

Parallel.For(1, 5, delegate(int i)
    {
        sum = sum * i;
    });

Result of parallel = 24

Result of normal = 120

  • 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-27T07:41:40+00:00Added an answer on May 27, 2026 at 7:41 am

    The answer in parallel version is arbitrary because sum and result are accessed and modified by different threads. So what you should do is separating between computing each step and summing up results. To be able to sum up results correctly, you need to obtain a lock so that threads modify sum exclusively. One way to fix could be:

    long sum = 0;
    object monitor = new object(); 
    Parallel.For(1, 11, () => 0L, (i, state, local) => 
    { 
        long result = 1;
    
        for (long j = 1; j <= i; j++)
        {
            result = result*j;
        } 
        return local + result;
    }, local => { lock (monitor) sum += local; }); 
    

    Note that you rarely need two nested Parallel.For loops, because they often lead to bad performance. So one Parallel.For loop at the outermost level and keeping inner for loops as they are is recommended. Also to obtain some speedup, you have to test with numbers much bigger than 10.

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

Sidebar

Related Questions

I am having the same problem as this post but the answer does not
I'm trying to understand whether the answer to the following question is the same
Which one of the following selectors has better performance, if not the same $('#location_form
100['toString'] //does not fail 100.toString //fails why? 100.toString is not same as 100.toString() .
The output of the fist System.out.println() is not same as the second System.out.println() What
[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
first of all: this is not the same as this . The ModelBackend has
I have two properties files that are not the same and I need to
I know that the f# list is not the same at the c# List.

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.