Back to Home

Math Problem Codes

Parabola visualization implemented in 7 different programming languages. Each version generates a graphical representation of y = x² using ASCII characters.

C++ Parabola Visualization

C++ implementation using iostream and cmath

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    const int width = 61;
    const int height = 20;

    for (int row = 0; row < height; ++row) {
        for (int col = 0; col < width; ++col) {
            double x = (double)col / (width - 1) * 2.0 - 1.0; // -1..1
            double y = x * x;                                  // 0..1
            int yRow = (int)round((1.0 - y) * (height - 1));
            cout << (yRow == row ? '*' : ' ');
        }
        cout << '\n';
    }
    return 0;
}

How to Run:

  1. Save as parabola.cpp
  2. Compile: g++ parabola.cpp -o parabola
  3. Run: ./parabola (Linux/Mac) or parabola.exe (Windows)

JavaScript Parabola Visualization

Node.js compatible JavaScript implementation

const width = 61;
const height = 20;

for (let row = 0; row < height; row++) {
  let line = "";
  for (let col = 0; col < width; col++) {
    const x = (col / (width - 1)) * 2 - 1;   // -1..1
    const y = x * x;                         // 0..1
    const yRow = Math.round((1 - y) * (height - 1));
    line += (yRow === row) ? "*" : " ";
  }
  console.log(line);
}

How to Run:

  1. Save as parabola.js
  2. Run with Node.js: node parabola.js
  3. Or run directly in browser console

Python Parabola Visualization

Python 3 implementation with clean list comprehensions

width = 61
height = 20

for row in range(height):
    line = []
    for col in range(width):
        x = (col / (width - 1)) * 2 - 1     # -1..1
        y = x * x                            # 0..1
        y_row = round((1 - y) * (height - 1))
        line.append('*' if y_row == row else ' ')
    print(''.join(line))

How to Run:

  1. Save as parabola.py
  2. Run: python parabola.py
  3. Or: python3 parabola.py

Lua Parabola Visualization

Lua implementation using table concatenation

local width = 61
local height = 20

for row = 0, height - 1 do
    local line = {}
    for col = 0, width - 1 do
        local x = (col / (width - 1)) * 2 - 1   -- -1..1
        local y = x * x                         -- 0..1
        local yRow = math.floor((1 - y) * (height - 1) + 0.5)
        if yRow == row then
            table.insert(line, "*")
        else
            table.insert(line, " ")
        end
    end
    print(table.concat(line))
end

How to Run:

  1. Save as parabola.lua
  2. Run: lua parabola.lua
  3. Requires Lua interpreter installed

Lisp Parabola Visualization

Common Lisp implementation using dotimes loops

(defun draw-parabola ()
  (let ((width 61)
        (height 20))
    (dotimes (row height)
      (let ((line (make-string width :initial-element #\Space)))
        (dotimes (col width)
          (let* ((x (- (* (/ (float col) (1- width)) 2.0) 1.0)) ; -1..1
                 (y (* x x))                                   ; 0..1
                 (y-row (round (* (- 1 y) (1- height)))))
            (when (= y-row row)
              (setf (char line col) #\*))))
        (format t "~A~%" line)))))

(draw-parabola)

How to Run:

  1. Save as parabola.lisp
  2. Run with Common Lisp (e.g., SBCL, CLISP): clisp parabola.lisp
  3. Or load into Lisp REPL

TCL Parabola Visualization

TCL/Tk script implementation

set width 61
set height 20

for {set row 0} {$row < $height} {incr row} {
    set line ""
    for {set col 0} {$col < $width} {incr col} {
        set x [expr {double($col)/($width-1)*2.0 - 1.0}]   ;# -1..1
        set y [expr {$x*$x}]                               ;# 0..1
        set yRow [expr {round((1.0 - $y)*($height-1))}]
        if {$yRow == $row} {
            append line "*"
        } else {
            append line " "
        }
    }
    puts $line
}

How to Run:

  1. Save as parabola.tcl
  2. Run: tclsh parabola.tcl
  3. Or: wish parabola.tcl

Expected Output (All Languages):

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

All 7 implementations produce this exact ASCII art representation of y = x² across a 61×20 character grid, demonstrating the same mathematical logic across different programming languages.