Please select a topic

Change record size

CICS

Copy files

CSV files

DB2 / SQL

Delete Files

Dynamic Allocation

File Allocation

GDGs

General

Hardware

ISPF

JavaScript

PDS(E)

Print files

Python

RACF

Rename files

SDSF

SORT

TSO/E

Utilities

VSAM

WEB

z/OS UNIX

Calculating the sum of a sequence of consecutive numbers



In February there was a post on Linkedin which contained a question from an interview and sort comment. The question stipulated that an Assembler program be written to calculate the sum of a sequence of numbers, i.e. 1,2,3,4,5,6,7, to n, but that there was to be no loop.

Initially I saw the lack of iterative processing as a problem, but then somebody posted a formula sum = (n * (n+1))/2.

It dawned on me that the exercise is useful in demonstrating various techniques in different languages so these are some offerings for edification and delight.

Original version

 

Modified version


These examples address the question directly, accepting the sequence end number as input but without any data validation.

 


These examples accept two numbers representing the start and end of the sequence, and the numbers are validated.