[Cobolforgcc-devel] Fw: Fun with Kaprekar number in COBOL
Status: Pre-Alpha
Brought to you by:
timjosling
From: William M. K. <wm...@ix...> - 2000-11-16 22:10:05
|
From comp.lang.cobol - in case you are interested (or want to be "amused"). You may want to "store" this in your test-bucket for when you do an ISO 200x conforming compiler. "Herwig Huener" <Herwig.Huener!@!fujitsu-siemens.com> wrote in message news:<8v131j$3s3$1...@ne...>... > 2000-11-16 17:47:27 MET > > Hi, > > below is a ready-to-go program conforming to the new > upcoming Cobol Standard. Actually, it is very few > Cobol - most stuff is directives. Purpose: a 4-digit > number in which not all digits are the same ("1111" etc) > is put into a compiler-variable and > digested by the simple well-known algorithm: > Arange the digits to make the largest and the smallest > number, subtract those two from each other and > repeat all that at most seven times over. You will > always arrive at the number 6174. > > (Strange enough that this simple property of > 4-digit numbers was discovered as late as 1949!) > > If one wants a real tough test-program for > a Cobol-compiler for the new standard, repeat > the code below for all possible 4-digits numbers > - things could be made easier by using > library elements - and you will force your > compiler to chew through about 5 million lines > of code. > > Next project is to compute Pi with directives - which > is more difficult because compiler-variables may not > have floating-point values ... > > Have fun. Herwig > > cut here: > > ------------------------------------------------------------------- > > ID DIVISION. > PROGRAM-ID. QDIR045. > environment division. > CONFIGURATION SECTION. > SPECIAL-NAMES. > TERMINAL IS TV. > INPUT-OUTPUT SECTION. > DATA DIVISION. > WORKING-STORAGE SECTION. > 01 error-counter pic 9999. > 01 correct-counter pic 9999. > PROCEDURE DIVISION. > > move zero to error-counter. > move zero to correct-counter. > > display "***************************************" upon tv. > display "* QDIR045: 2000-11-16 17:25:00 MEZ *" upon tv. > display "* QDIR045: Conditional Compilation *" upon tv. > display "* QDIR045: Kaprekar! *" upon tv. > display "***************************************" upon tv. > > >> define kaprekar as 9876 > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> define a as kaprekar / 1000 > >> define b as kaprekar / 100 > >> define c as kaprekar / 10 > >> define d as kaprekar > > >> define aa as a > >> define bb as b - 10 * a > >> define cc as c - 10 * b > >> define dd as d - 10 * c > > >> define a as aa override > >> define b as bb override > >> define c as cc override > >> define d as dd override > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if c < d > >> define qqq as c > >> define c as d override > >> define d as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> if b < c > >> define qqq as b > >> define b as c override > >> define c as qqq override > >> define qqq as off > >> end-if > > >> if a < b > >> define qqq as a > >> define a as b override > >> define b as qqq override > >> define qqq as off > >> end-if > > >> define max as a * 1000 + b * 100 + c * 10 + d > >> define min as d * 1000 + c * 100 + b * 10 + a > > >> define kaprekar as max - min override > > >> define max as off > >> define min as off > > >> define a as off > >> define b as off > >> define c as off > >> define d as off > > >> define aa as off > >> define bb as off > >> define cc as off > >> define dd as off > > *************************************************************** > > >> if 6174 is equal to kaprekar > add 1 to correct-counter. > >> else > add 1 to error-counter > >> end-if > > *************************************************************** > > if error-counter not equal to zero > display " QDIR045: FAILED 1 !" upon tv > stop run. > > if correct-counter not equal to 1 > display " QDIR045: FAILED 2 !" upon tv > stop run. > > display " QDIR045: PASSED." upon tv. > stop run. |