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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:12:53+00:00 2026-05-28T03:12:53+00:00

1) Sum := 0; for J in 1 .. N loop Sum := Sum

  • 0

1)

Sum := 0;
for J in 1 .. N loop
Sum := Sum + J;
end loop;

2)

Sum := ((N + 1) * N) / 2;

enter image description here

I’ve read that the first(left) solution is more “general” – applicable to a wide range of values ​​- than second(right) solution. What does “general” mean – can be computed without overflow?

  • 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-28T03:12:54+00:00Added an answer on May 28, 2026 at 3:12 am

    I think “more general” must mean “can be computed without overflow for a larger range of numbers”.

    The intermediate product in (2) will overflow at 2^31 – 1 (for a 32-bit Integer as you will get on most modern machines), which means that the largest legal result will be somewhat less than 2^30 – 1. (1) will let you continue almost as far again (if you can wait that long!)

    This program explores the limits:

    with Ada.Exceptions;
    with Ada.Text_IO; use Ada.Text_IO;
    procedure Summation is
       N : Integer := 1;
       Sum : Integer;
    begin
       loop
          Put (Integer'Image (N) & " => ");
          Sum := ((N + 1) * N) / 2;
          Put_Line (Integer'Image (Sum));
          N := N + 1;
       end loop;
    exception
       when E : others =>
          Put_Line (Ada.Exceptions.Exception_Message (E));
    end Summation;
    

    and if you compile with gnatmake -gnato summation.adb and run it it ends with

    46337 =>  1073581953
    46338 =>  1073628291
    46339 =>  1073674630
    46340 =>  1073720970
    46341 => summation.adb:9 overflow check failed
    

    If you leave out the -gnato, so that GNAT doesn’t do numeric overflow checks (a regrettable default, chosen as I remember for efficiency) this happens:

    46337 =>  1073581953
    46338 =>  1073628291
    46339 =>  1073674630
    46340 =>  1073720970
    46341 => -1073716337
    46342 => -1073669995
    46343 => -1073623652
    

    I suppose you could get the longer range by dividing whichever of N and N + 1 was even (one clearly must be!) by 2 before doing the multiplication.

    This isn’t really an Ada problem (though -gnato makes it easier to see when things go wrong than might happen with some other languages), and it’s certainly not a factorial! Is it possible to edit the title?

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

Sidebar

Related Questions

How can I sum all of the values in an array of arrays? Sample
I would like to create a safe sum extension method that would have the
I want to sum acidic as the for loop runs and then print out
I have an application where part of the inner loop was basically: double sum
What does this instruction vector=[vector,sum(othervector)] does in matlab inside a while loop like: vector=[];
I want to loop a calculation that is based on users params. Example in
SELECT sum(TotalHoursM) + (TotalHoursT) + (TotalHoursW) + (TotalHoursTH) + (TotalHoursF) AS TOTAL FROM LeaveRequest
Simple sum of digits code. It compiles but when executed, the last cout gives
To sum it up, there are two basic trains of thought: The private field
SELECT SUM(a.Clicks) AS Clicks, SUM(b.NoOfUsers) Users, c.WEEK_NUM, b2.ALL_TASKS FROM (SELECT SUM(CLICK_CNT) AS Clicks, TO_CHAR(RQST_DT,'YYYY-MM-DD')

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.