MAKE THE MATHEMATICS YOURSELF

Euler’s luminous parabola in C

A complete program. Standard C, a compiler, and the rule behind the shape.

Output generated by this C program: Euler’s luminous parabola
Generated by the C program below Download image ↗
FROM SOURCE TO SHAPE

Run it in three steps.

  1. Save the source.

    Download euler-quadratic.c into a folder on your computer.

  2. Compile it.

    In that folder, run this with GCC or Clang:

    cc -std=c11 -O2 euler-quadratic.c -lm -o euler-quadratic
  3. Make the image.
    ./euler-quadratic

    Open euler-quadratic.svg in a browser to see the result.

On Windows with GCC, name the executable euler-quadratic.exe and run it from the same folder.

THE RULE IN THE PROGRAM

How the picture is built

Evaluate k*k+k+c and place each output on the square integer spiral. Prime outputs glow and composites are dim.

Make it your own

terms=50, constant=41

The familiar 40-prime run is k=0 through 39 only; k=40 gives 41 squared. Keep constant positive and terms small enough for int arithmetic. Each run produces one image; use Graphic mode for the interactive animation.

The complete source

euler-quadratic.c · 113 lines · no graphics libraries
/* Arithmos: euler-quadratic
 * Compile: cc -std=c11 -O2 euler-quadratic.c -lm -o euler-quadratic
 * Run:     ./euler-quadratic
 * Output:  euler-quadratic.svg (open this file in a browser)
 * Optional output path: ./euler-quadratic my-image.svg
 * Edit the constants in draw() to explore another case.
 */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static FILE *nt_out;
#define NT_PI 3.14159265358979323846

/* The small SVG writer keeps this program free of graphics dependencies.
 * Coordinates are pixels on a 1000 x 700 drawing surface.
 * t runs from 0 to 1 through mint, blue, rose, and gold.
 */
static inline void nt_color(double t, char hex[8]) {
    const double stops[4][3] = {
        {91,227,201}, {128,146,240}, {218,138,220}, {244,200,127}
    };
    t = fmax(0.0, fmin(1.0,t)) * 3.0;
    int band = (int)fmin(2.0,floor(t));
    double blend = t - band;
    int r[3];
    for (int k=0;k<3;k++) r[k]=(int)lround(stops[band][k]*(1.0-blend)+stops[band+1][k]*blend);
    snprintf(hex,8,"#%02x%02x%02x",r[0],r[1],r[2]);
}
static inline void nt_line(double x,double y,double X,double Y,double t,double alpha,double width) {
    char color[8];nt_color(t,color);
    fprintf(nt_out,"<path d=\"M%.3f %.3f L%.3f %.3f\" fill=\"none\" stroke=\"%s\" stroke-opacity=\"%.3f\" stroke-width=\"%.3f\"/>\n",x,y,X,Y,color,alpha,width);
}
static inline void nt_dot(double x,double y,double r,double t,double alpha) {
    char color[8];nt_color(t,color);
    fprintf(nt_out,"<circle cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\" fill=\"%s\" fill-opacity=\"%.3f\"/>\n",x,y,r,color,alpha);
}
static inline void nt_circle(double x,double y,double r,double t,double alpha,double width) {
    char color[8];nt_color(t,color);
    fprintf(nt_out,"<circle cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\" fill=\"none\" stroke=\"%s\" stroke-opacity=\"%.3f\" stroke-width=\"%.3f\"/>\n",x,y,r,color,alpha,width);
}
static inline void nt_rect(double x,double y,double w,double h,double t,double alpha) {
    char color[8];nt_color(t,color);
    fprintf(nt_out,"<rect x=\"%.3f\" y=\"%.3f\" width=\"%.3f\" height=\"%.3f\" fill=\"%s\" fill-opacity=\"%.3f\"/>\n",x,y,w,h,color,alpha);
}
static inline void nt_text(double x,double y,const char *text) {
    fprintf(nt_out,"<text x=\"%.3f\" y=\"%.3f\" fill=\"#ededf3\" font-family=\"monospace\" font-size=\"16\">",x,y);
    for (;*text;text++) {
        if (*text=='&') fputs("&amp;",nt_out);
        else if (*text=='<') fputs("&lt;",nt_out);
        else if (*text=='>') fputs("&gt;",nt_out);
        else fputc(*text,nt_out);
    }
    fputs("</text>\n",nt_out);
}
static inline int nt_gcd(int a,int b) {a=abs(a);b=abs(b);while(b){int r=a%b;a=b;b=r;}return a;}
static inline int nt_prime(int n) {if(n<2)return 0;for(int d=2;d<=n/d;d++)if(n%d==0)return 0;return 1;}

/* Map polynomial outputs to exactly the same square spiral as Ulam. */
static void euler_xy(int k,int *x,int *y) {
    if(k==1){*x=0;*y=0;return;}
    int r=(int)ceil((sqrt((double)k)-1)/2),s=2*r,d=(2*r+1)*(2*r+1)-k;
    if(d<s){*x=r-d;*y=-r;}
    else if(d<2*s){*x=-r;*y=-r+d-s;}
    else if(d<3*s){*x=-r+d-2*s;*y=r;}
    else {*x=r;*y=r-d+3*s;}
}
static void draw(void) {
    const int terms=50, constant=41; /* Website controls: terms and c. */
    int xmin=100000,xmax=-100000,ymin=100000,ymax=-100000;
    for(int k=0;k<terms;++k){
        int x,y; euler_xy(k*k+k+constant,&x,&y);
        if(x<xmin)xmin=x;if(x>xmax)xmax=x;
        if(y<ymin)ymin=y;if(y>ymax)ymax=y;
    }
    double sx=820.0/fmax(1,xmax-xmin),sy=530.0/fmax(1,ymax-ymin);
    double scale=fmin(sx,sy), mx=(xmin+xmax)/2.0,my=(ymin+ymax)/2.0;
    for(int x=xmin;x<=xmax;++x) for(int y=ymin;y<=ymax;++y)
        nt_dot(500+(x-mx)*scale,345-(y-my)*scale,.55,.2,.12);
    for(int k=0;k<terms;++k){
        int value=k*k+k+constant,x,y; euler_xy(value,&x,&y);
        double px=500+(x-mx)*scale,py=345-(y-my)*scale;
        int prime=nt_prime(value);
        nt_dot(px,py,3.2,prime?.8:.05,prime?1:.13);
        if(constant==41 && k==40){
            nt_circle(px,py,7,.95,1,1.2);
            nt_text(px+12,py-9,"n=40: 1681 = 41 squared");
        }
    }
    nt_text(45,675,"Outputs of k*k+k+c on the square spiral. Prime values glow; composites are dim.");
}

int main(int argc, char **argv) {
    if (argc > 2) {
        fprintf(stderr, "Usage: %s [OUTPUT.svg]\n", argv[0]);
        return EXIT_FAILURE;
    }
    const char *filename = argc == 2 ? argv[1] : "euler-quadratic.svg";
    nt_out = fopen(filename, "wb");
    if (!nt_out) { perror(filename); return EXIT_FAILURE; }
    fputs("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1000\" height=\"700\" viewBox=\"0 0 1000 700\">\n"
          "<rect width=\"1000\" height=\"700\" fill=\"#171721\"/>\n", nt_out);
    draw();
    fputs("</svg>\n", nt_out);
    int failed = ferror(nt_out);
    if (fclose(nt_out) != 0) failed = 1;
    if (failed) { fputs("Could not finish writing the image.\n", stderr); return EXIT_FAILURE; }
    printf("Wrote %s\n", filename);
    return EXIT_SUCCESS;
}
All 40 explorations, ready to compile.Download all C examples ↓