TIL but from youtube: Caboose

TIL but from youtube: Caboose

Introduction

One of my favourite things to do while coming back from home to work is watch Matt Parker videos, find his videos super fun and I learn alot from them. Every time when I see a video, I think of writing the code myself too in my own simpler way. That is what I did this time.

Caboose numbers

What super chad mathematician Euler found was that if we did n^2-n+41, for all numbers 1 to 40, resultant number would be a prime, it may not work for numbers after 41, but it does from 1 to 41, this is very fascinating, this is explored in very good detail in the numberphile video about it. I try to write code in python, go, rust to do the same. Also try to find the ratio between the number of primes to the number n to see if a number results in more number of primes or non primes.

Key Steps and Logic Involved

  1. Iterate through a sequence of integers.

  2. Apply the formula n2−n+41n^2 - n + 41n2−n+41.

  3. Check if the resulting number is prime.

  4. Count the number of primes and calculate the ratio.

Algorithm in Rust

use std::f64;

fn is_prime(n: i32) -> bool {
    if n <= 1 {
        return false;
    }
    let sqrt_n = (n as f64).sqrt() as i32;
    for i in 2..=sqrt_n {
        if n % i == 0 {
            return false;
        }
    }
    true
}

fn is_caboose(n: i32) -> bool {
    for i in 1..=n {
        let x = i * i - i + 41;
        if !is_prime(x) {
            return false;
        }
    }
    true
}

fn caboose_prime_ratio(n: i32) -> f64 {
    let mut prime_count = 0;
    for i in 1..=n {
        let x = i * i - i + 41;
        if is_prime(x) {
            prime_count += 1;
        }
    }
    prime_count as f64 / n as f64
}

fn main() {
    let n = 100;
    for i in 1..n {
        if is_caboose(i) {
            println!("{} is Caboose", i);
        } else {
            println!("{} is not Caboose", i);
        }
        println!("Caboose ratio for {} is {:.2}", i, caboose_prime_ratio(i));
    }
}

Algorithm in python

import math
def isPrime(n):
    for i in range(2, int(math.sqrt(n)+1)):
        if(n%i==0):
            return False
    return True

def isCaboose(n):
    prime = 0
    for i in range(1, n+1):
        x = i**2 - i + 41
        if not (isPrime(x)):
            return False
        prime += 1
    return True

def caboosePrimeRatio(n):
    prime_count = 0
    for i in range(1, n + 1):
        x = i**2 - i + 41
        if isPrime(x):
            prime_count += 1
    return prime_count / n

n = 100
for i in range(1,n):
    if(isCaboose(i)):
        print(f"{i} is Caboose")
    else:
        print(f"{i} is not Caboose")
    print(f"Caboose ratio for {i} is {caboosePrimeRatio(i)}")

Algorithm in go

package main

import (
    "fmt"
    "math"
)

func isPrime(n int) bool {
    if n <= 1 {
        return false
    }
    sqrtN := int(math.Sqrt(float64(n)))
    for i := 2; i <= sqrtN; i++ {
        if n%i == 0 {
            return false
        }
    }
    return true
}

func isCaboose(n int) bool {
    for i := 1; i <= n; i++ {
        x := i*i - i + 41
        if !isPrime(x) {
            return false
        }
    }
    return true
}

func caboosePrimeRatio(n int) float64 {
    primeCount := 0
    for i := 1; i <= n; i++ {
        x := i*i - i + 41
        if isPrime(x) {
            primeCount++
        }
    }
    return float64(primeCount) / float64(n)
}

func main() {
    n := 100
    for i := 1; i < n; i++ {
        if isCaboose(i) {
            fmt.Printf("%d is Caboose\n", i)
        } else {
            fmt.Printf("%d is not Caboose\n", i)
        }
        fmt.Printf("Caboose ratio for %d is %.2f\n", i, caboosePrimeRatio(i))
    }
}