Jump to content

The first program


PeterCanessa Oh
 Share

You are about to reply to a thread that has been inactive for 4469 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I am, in my whimsical way, developing a new programming language/compiler/IDE in which the focus is ease of learning and use rather than, necessarily, efficiency. There are a number of reasons why 'standard' languages are difficult; their syntax, paradigms and concepts, for example. The obvious effects of which are that it takes people a long time to learn to program and even experienced developers make lots of mistakes. As a generalisation the problem is that all these languages were designed by mathematicians and/or engineers and these are not the people who are trying to use them. Examples from LSL, for instance; 'normal' people do not talk about 'integers' and 'floats', let alone radians, angles that start East and increase anti-clockwise or - haha, of course - quaternions.

As well as ditching the prejudices of existing designs I thought I'd abandon the 'traditional' first program as well.  "Hello World" just doesn't do it for me, so I looked-up the first ever program, to re-create it.  Oh the irony; it's very mathematical :-0

 

"In note G of Ada Lovelace's notes on the analytical engine from 1842,
Lovelace describes an algorithm for generating Bernoulli numbers with Babbage's
machine. As a result, the Bernoulli numbers have the distinction of being the
subject of the first computer program."

(http://en.wikipedia.org/wiki/Bernoulli_number)

 

That link gives, amongst all the mathematical details, this code for generating the Bernoulli numbers:

 

Algorithm Akiyama–Tanigawa algorithm for Bn
  Input: Enter integer n.
  Output: Bn.

  for m from 0 by 1 to n do
    A[m] ← 1/(m+1)
    for j from m by -1 to 1 do
      A[j-1] ← j×(A[j-1] - A[j])
  return A[0] (which is Bn)

 

Question 1: What language is that?  I think it's Algol, but I'm not sure.  The '←' for assignment should be quite distinctive.

Question 2: Am I correct in translating "for j from m by -1 to 1 do" as (in LSL) "for(j = m; j > 0; j--){"

Now the biggies:
Question 3: What's wrong with this LSL version? (Apart from not being inlined Rolig and Ela!)

 

float Bernoulli(integer Number){
    if(Number == 0){
        return 1.0;
    }else if(Number == 1){
        return 0.5;
    }else if((Number < 0) || (Number % 2)){
        return 0.0;
    }else{
        list A;
        integer J;
        integer M;
        for(M = 0; M <= Number; M++){
            A += [1.0 / (M + 1.0)];
            for(J = M; J > 0; J--){
                A = llListReplaceList(A, [J * (llList2Float(A, J-1) - llList2Float(A, J))], J-1, J-1);
            }
        }
        return llList2Float(A, 0);
      }
}

default{
    state_entry(){
        integer C;
        for(C = 2; C < 20; C += 2){
            llOwnerSay("B(" + (string) C + ") = " + (string) Bernoulli(C));
            llSleep(0.2);	// Seems to help chat stay in order
        }
    }
}

 

The return paths for < 0, 0, 1 and odd-numbers > 1 are all correct but the even-number results veer-off wildy after B(8) (which is 'close enough', even if it doesn't agree with wikipedia).

 

  • B(2) = 0.166667
  • B(4) = -0.033333
  • B(6) = 0.023818
  • B(8) = -0.032196
  • B(10) = 0.299849
  • B(12) = 29.289020
  • B(14) = 5489.975000
  • B(16) = 1966391.000000
  • B(18) = 2113044000.000000

(Optional bonus) Question 4: Is this PHP a valid translation of the LSL? (Yes, I am playing around with cross-compilers)

 

<?php
	function Bernoulli($Number){
	    if($Number == 0){
	        return 1.0;
	    }elseif($Number == 1){
	        return 0.5;
	    }elseif(($Number < 0) || (($Number % 2) == 1)){
	        return 0.0;
	    }else{
	        for($M = 0; $M <= $Number; $M++){
	            $A[$M] = 1.0 / ($M + 1.0);
	            for($J = $M; $J > 0; $J--){
	                $A[$J-1] = $J * ($A[$J-1] - $A[$J]);
	            }
	        }
	        return $A[0];
	      }
	}

	for($C = 0; $C < 21; $C += 2){
		echo "B(".$C.") = ".Bernoulli($C)."</br>\n"; 
	}
?>

 

Question 5:  Why does it give a different set of (wrong) answers?

 

  • B(2) = 0.16666666666667
  • B(4) = -0.033333333333334
  • B(6) = 0.023809523809568
  • B(8) = -0.033333333331542
  • B(10) = 0.075757575267314
  • B(12) = -0.25311370332041
  • B(14) = 1.1666676791536
  • B(16) = -7.0685020745223
  • B(18) = 69.99698015617

And finally:
Question 6: Good grief! I can't believe the first program was really for something so awkward to calculate. Where is the missing "Felicitous greetings, to Her Majesty, British nationals and citizens of the Empire" program?

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4469 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...