Eueler Problem 12

Today’s problem reads:

What is the value of the first triangle number to have over five hundred divisors

And my solution looks like:

console.log(
	triangleNumbers()
		.find(n => divisors(n).length > 500)
)

It delights me when the solution to a problem reads just like the problem description.

Today was a chance to code another generator function, this time for triangle numbers:

function triangleNumber(n: number): number {
    return n * (n + 1) / 2
}
 
function* triangleNumbers() {
    let i = 1;
    while (true) {
        yield triangleNumber(i)
        i += 1
    }
}

And a divisor function too:

function divisors(n: number): number[] {
    if (n === 1) {
        return [1]
    }
 
    const result: number[] = [];
 
    for (let i = 1; i <= Math.sqrt(n); i++) {
        if (n % i === 0) {
            result.push(i)
            result.push(n / i)
        }
    }
 
    return result;
}

Since I’m likely to need those again they get moved into shared folders and the complete solution for today looks like:

import { triangleNumbers } from "../util/sequence/triangle_numbers.ts";
import { divisors } from "../util/util.ts";
 
console.log(
    triangleNumbers()
        .find(n => divisors(n).length > 500)
)

Missing Semester Meeting 2

Is  chmod a+x the same as chmod ugo+x ?

Yes!

[nicole@hera scripts]$ touch hi.sh
[nicole@hera scripts]$ touch hi2.sh
[nicole@hera scripts]$ ls -l
total 0
-rw-r--r-- 1 nicole nicole 0 Nov 14 14:35 hi2.sh
-rw-r--r-- 1 nicole nicole 0 Nov 14 14:35 hi.sh
[nicole@hera scripts]$ chmod a+x hi.sh
[nicole@hera scripts]$ ls -l
total 0
-rw-r--r-- 1 nicole nicole 0 Nov 14 14:35 hi2.sh
-rwxr-xr-x 1 nicole nicole 0 Nov 14 14:35 hi.sh
[nicole@hera scripts]$ chmod ugo+x hi2.sh
[nicole@hera scripts]$ ls -l
total 0
-rwxr-xr-x 1 nicole nicole 0 Nov 14 14:35 hi2.sh
-rwxr-xr-x 1 nicole nicole 0 Nov 14 14:35 hi.sh

tldr command

The tldr command gives examples of how to use a command and is useful as an adjunct to man pages:

[nicole@hera Documents]$ tldr chmod

  chmod

  Change the access permissions of a file or directory.
  More information: https://www.gnu.org/software/coreutils/chmod.

  Give the [u]ser who owns a file the right to e[x]ecute it:

    chmod u+x path/to/file

  Give the [u]ser rights to [r]ead and [w]rite to a file/directory:

    chmod u+rw path/to/file_or_directory

  Remove e[x]ecutable rights from the [g]roup:

    chmod g-x path/to/file

  Give [a]ll users rights to [r]ead and e[x]ecute:

    chmod a+rx path/to/file

  Give [o]thers (not in the file owner's group) the same rights as the [g]roup:

    chmod o=g path/to/file

  Remove all rights from [o]thers:

    chmod o= path/to/file

  Change permissions recursively giving [g]roup and [o]thers the ability to [w]rite:

    chmod -R g+w,o+w path/to/directory

  Recursively give [a]ll users [r]ead permissions to files and e[X]ecute permissions to sub-directories within a directory:

    chmod -R a+rX path/to/directory

Also helpful that tldr -u updates the hint pages so they’re available offline

fzf command

fzf - https://github.com/junegunn/fzf is also super cool!

It searches through whatever it receives on STDIN and writes the selected line to STDOUT. I immediately need this! I story my creative coding with a date prefix but often want to find sketches I’ve done that contain a word. Here’s searching for the word walker and changing into that directory.

Terminal App

I’m trying out kitty to see how it feels. It’s hard to exactly put my finger on why the built in XFCE terminal feels slow and the text feels fuzzy. So far kitty seems sharper. I’m not sure if it feels faster. I really like that I can keep my config all organized in ~/.config/ with kitty.

Bouncing Balls

This is the first example from Nature of Code Chapter 2. It implements bouncing balls without using vectors

Bouncing Balls with Vectors

Bouncing Balls in 3D

The math is right here but I’m not sure the illusion is convincing. The balls scale up as they get close to the viewer and diminish in size as they move away. I also have them getting darker as they get away (this was kind of a pain):

Here’s the drawing code:

function draw() {
  update();
 
  background("#F8FFE5");
 
  const sortedBalls = balls.sort((a, b) => a.position.z - b.position.z);
  for (const ball of sortedBalls) {
    noStroke();
    const targetColor = color(ball.color);
 
    let colorScale = map(ball.position.z, 0, canvas.depth, 1, 0.4);
    targetColor.setRed(red(targetColor) * colorScale);
    targetColor.setGreen(green(targetColor) * colorScale);
    targetColor.setBlue(blue(targetColor) * colorScale);
 
    const scaleFactor = map(ball.position.z, 0, canvas.depth, 1, 0.1);
    fill(targetColor);
    circle(ball.position.x, ball.position.y, ball.size * scaleFactor);
  }
}

End of day

Today was a really good day! I feel like I’m getting to know more and more people which makes being in the hub more and more comfortable and exciting. I’m starting to feel the pangs of knowing that next week is my last week in NYC (unless I try to come back for the last week of batch. I might!)

Notebook page from today: https://notes.nicole.computer/daily/2024-11-14

Worked on Today

  • We had study group 2 for the “Missing Semester” and oh gosh was there a difficulty spike. @rpasetes gave a great explanation of setting permission bits. Things I’m excited about the tldr command for cheatsheets, nnn as a file manager (maybe replacing mc for me) and fzf for having a fuzzy finder
  • I did a Euler problem and I’m suuuuuper happy with how my solution reads (solution here: https://github.com/nicolecomputer/euler/blob/main/012/main.ts):
console.log(
    triangleNumbers()
        .find(n => divisors(n).length > 500)
)

Tomorrow

I also did a lot of doodling on the word “bouncy” today. I think I want to do some word art where the word bouncy moves in a way that feels right for the word. I have a notebook full of ideas and I’d like to implement them.

I’d also like to finish up the exercises for Chapter 1 in NOC tomorrow.