# -*- coding: utf-8 -*-
Program to print pineapple pen apple pen, playing with strings and lists.
@author: mathew

print("PPAP Program")

p = 'pen'
a = 'apple'

P = 'pineapple'

if 'pen' in p and 'apple' in a:
ap = [a, p]

if 'pen' in p and 'pineapple' in P:
pP = [p, P]


PPAP = (pP, ap,)







for string in reversed(ap):

for string in Pp:

for string in reversed(ap):
print(string, end='')

for string in Pp:
print(string, end='')

Thanks to Kelsey Ellison for introducing me to this earworm.


compiling gcc 4.1.2 to get gfortran-4.1.2

I need to do this to use thermocalc tq on my linux computer (debian squeeze).

1) Download
I think I downloaded and untarred (I had these on my computer from previous time)
gcc-fortran-4.1.2.tar.gz (not sure this is needed — I think other folder contains everything!?)

2) Install prerequisites

3) Make a new directory in which you will compile gcc (first trick you can learn from reading the documentation — unlike most other packages it is not intended to build gcc in the src directory)

4) Configure
enter DIR and run

/blah/blah/gcc-4.1.2/configure --prefix=/opt/gcc412 
--program-suffix=412 --enable-shared 
--enable-threads=posix --enable-checking=release 
--with-system-zlib --disable-libunwind-exceptions 
--enable-__cxa_atexit --disable-multilib

5) Avoid a bug
then edit the make file to remove error about makeinfo

MAKEINFO = /blah/missing makeinfo
needs to be changed to (make sure you actually have makeinfo! which is in texinfo package I think)
MAKEINFO = makeinfo

6) Compile and install
I think after that you can just compile with make or make all

More sophisticated things are possible build gcc, to bootstrap the compiler. I don’t think this is necessary for what I need.

Making destination be /opt means that gcc should not appear in the PATH with higher priority than /usr/bin, as will happen if you place in /usr/local. Suffix of 412 means you will get binaries with different name than gcc so it should not allow any confusion to occur.

Octave/ Matlab is fun – Enter the Matrix

f = rot90((diag((ones(5,1)))) + hankel(zeros(5,1),2*(ones(5,1))),1)
warning: hankel: column wins anti-diagonal conflict
f =

0 2 2 2 3
0 0 2 3 2
0 0 1 2 2
0 1 0 0 2
1 0 0 0 0

ans =

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

ans =


ans =

0 0 0 0 1 0 0
0 0 0 0 0 2 0
0 0 0 0 0 0 3
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

ans =

0.141505 0.100645 0.200515 0.552900
0.163904 0.081520 0.505508 0.356016
0.458187 0.275491 0.472148 0.434868
0.988882 0.267269 0.680475 0.424024

Java Triangle

Some Java for your Christmas present…

Here is a short Java program to calculate triangular numbers,
(the sequence: 1 3 6 10 15 21 … ).

* Program to calculate triangular numbers
* inefficiently
* Mathew Peet
* Christmas 2009

public class Triangular {
public static long tri(int n) {
int total = 1;
for (int i = 1; i <=n; i++)
total = total + (i+1);
return total;

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + tri(i));

My second g++/cpp program – Using a Makefile

I downloaded the example files from here:

This demonstration explains how to compile using the make file. In this example you split up the program into separate modules, and use a make file to compile. This has the advantage that after making a change, only that part of the program needs to be recompiled.

Read the instructions on the web page, after downloading you need to edit Point.h to prevent it being included twice.

#ifndef POINT_H
#define POINT_H
* File: Point.h
* Last Modified: January 31, 2000
* Topic: Modules, Separate Compilation, Using Make Files
* ----------------------------------------------------------------

#include // one of two
using namespace std; // two of two, yay!

class Point
Point(int xval, int yval);
void move(int dx, int dy);
int get_x() const;
int get_y() const;

int x;
int y;


otherwise you will get the error:
g++ -c -o main.o main.cpp
In file included from Rectangle.h:13,
from main.cpp:9:
Point.h:13: error: redefinition of 'class Point'
Point.h:14: error: previous definition of 'class Point'

The files you download are: main.cpp, Makefile, Point.cpp, Point.h, Rectangle.cpp, Rectangle.h

and the Makefile looks like this:

# Makefile for Separate Compilation Example

# *****************************************************
# Parameters to control Makefile operation

CXX = g++

# ****************************************************
# Entries to bring the executable up to date

main: main.o Point.o Rectangle.o
$(CXX) $(CXXFLAGS) -o main main.o Point.o Rectangle.o

main.o: Point.h Rectangle.h

Point.o: Point.h

Rectangle.o: Rectangle.h Point.h

after running the make command you will have a bunch of object files (.o) and an executable. If you edit Rectangle.cpp and run make again then only Rectangle.o needs to be made again. The programs fragments of binary are linked by the .h header files.

if you run without preventing the multiple inclusion you get this error:

$ make
g++ -c -o main.o main.cpp
In file included from Rectangle.h:13,
from main.cpp:9:
Point.h:13: error: redefinition of ‘class Point’
Point.h:14: error: previous definition of ‘class Point’
make: *** [main.o] Error 1

After correction you get this:

$ make
g++ -c -o main.o main.cpp
g++ -c -o Point.o Point.cpp
g++ -c -o Rectangle.o Rectangle.cpp
g++ -o main main.o Point.o Rectangle.o

Edit Rectangle.cpp and run again; (e.g. open and save to update time, or just type “touch Rectangle.cpp“.

e$ make
g++ -c -o Rectangle.o Rectangle.cpp
g++ -o main main.o Point.o Rectangle.o

My first g++/cpp program

test.cpp is a text file containing:


using namespace std;
int main()
cout<<"test test"<<endl;
return 0;

This is compiled using the command:
g++ -o test test.cpp


One tool I picked up from Clive Page’s Page on Fortran was ftnchek. This program is a static analyser. It enables checking or Fortran77 for semantic errors (rather than syntax errors), which usually aren’t reported by compilers. Once a Program compiles you can check for syntax errors by running ftnchek program.f

I found most of my errors are of this type.

    189             XTO400=XTO400+0.0001
Warning near line 189 col 26 file mucg83.f: promotion may not give desired
 precision: dble XTO400 + real const 0.0001

What I should have written is


Which means the fortran compiler knows the number involved is meant to be used in double precision. Since there is really no cost penalty to using ‘double’ precision rather than ‘single’ precision used with ‘Real’ numbers – and this might change depending on compiler.

I found the static analyser to be very useful for finding unused parts of a program, this might be useful to find errors, if you think those parts of the code are meant to do something! Or in cleaning up the code after making changes.

ftnchek can be downloaded from the ftnchek developers website, or installed in debian from the program repositories using apt-get, dselect or aptitude, etc.

ftnchek --version
FTNCHEK Version 3.3 November 2004 Patch Level 1