ProjectEulerRust

repository·master·Indexed 41 days ago

https://github.com/gifnksm/projecteulerrust

A collection of solutions to Project Euler problems implemented in Rust, featuring a suite of mathematical utilities. Includes tools for continued fractions, Pell equation solvers, prime number generation via PrimeSet, integer factorization, and combinatorial iterators for permutations and combinations.

Tokens
22.2K
Snippets
164
Records
176
Agent score
52%

What's inside ProjectEulerRust

  1. Run Project Euler problems

    master

    You can run the Project Euler solutions using cargo. You can either run all problems via the euler binary or run a specific problem by targeting its individual binary (e.g., p001 for problem 1). It is recommended to use the --release flag for performance, as many Project Euler problems are computationally intensive.

    # Run all problems
    cargo build --release
    cargo run --release --bin euler
    
    # Run a specific problem (e.g., problem 1)
    cargo run --release --bin p001
  2. Run tests and benchmarks

    master

    To ensure correctness and performance, you can run the project's test suite or benchmarks across the entire workspace using the following commands.

    # Run all tests in the workspace
    cargo run test --workspace
    
    # Run all benchmarks in the workspace
    cargo run bench --workspace
  3. Implement compatible problem executables for the euler CLI

    master

    To be compatible with the euler runner, individual problem executables must follow these requirements:

    1. Naming Convention: The executable filename must match the regex p[0-9][0-9][0-9] (e.g., p001, p002).
    2. CLI Interface: The executable must accept a --json flag.
    3. Output Format: When the --json flag is provided, the program must output a JSON object compatible with the SolverResult structure (used by the runner to calculate averages and totals).
  4. Hand ranking and comparison logic in Problem 54

    master

    The solver implements a Hand enum to represent different poker hand strengths. The ranking order (from lowest to highest) is:

    1. HighCard
    2. Pair
    3. TwoPairs
    4. ThreeOfAKind
    5. Straight
    6. Flush
    7. FullHouse
    8. FourOfAKind
    9. StraightFlush
    10. RoyalFlush

    Comparison is performed using the Ord trait, which first compares the rank of the hands and then uses the card values (suit and number) as tie-breakers to determine the winner.

    // Example of how hands are represented and compared
    // Hand::from_cards takes a slice of 5 cards
    let hand1 = Hand::from_cards(&[/* 5 cards */]);
    let hand2 = Hand::from_cards(&[/* 5 cards */]);
    
    match hand1.cmp(&hand2) {
        Ordering::Greater => println!("Player 1 wins"),
        Ordering::Less => println!("Player 2 wins"),
        Ordering::Equal => println!("Draw"),
    }
  5. Implement a Project Euler problem using the `problem!` macro

    master

    The problem! macro is the primary way to define a Project Euler solver. It handles initialization, execution, and provides a built-in test case. You can use it in two ways:

    1. Simple Solver: For problems that do not require external input files.
    2. File-based Solver: For problems that require downloading a resource file (e.g., a large dataset) from Project Euler. The macro will automatically attempt to download the file to a ./.cache directory if it doesn't exist.

    Note: The $answer placeholder should be the expected correct answer as a string, and $solver should be a function that returns the calculated answer as a String.

    // Simple solver
    problem!("12345", my_solver_function);
    
    // File-based solver
    problem!("12345", "input_data.txt", my_file_solver_function);
  6. Solve Project Euler Problem 33

    master

    This binary is a specialized solver for Project Euler Problem 33. It calculates the result using a mathematical approach based on digit patterns and outputs the final answer as a string. It is integrated into the project's common problem execution framework via the common::problem! macro.

    // This is a standalone binary provided by the repository.
    // To run it, use cargo:
    // cargo run --bin p033
  7. Solve Project Euler Problem 18

    master

    This binary is a solver for Project Euler Problem 18. It uses a hardcoded triangle input to compute the maximum sum of a path from top to bottom. The logic is implemented via the compute and parse functions, which process a string representation of a numeric triangle into a 2D vector and then perform a bottom-up dynamic programming calculation.

    cargo run --bin p018
  8. Solve Project Euler Problem 6

    master

    This binary solves Project Euler Problem 6 (Convex polyhedra). It calculates the difference between the square of the sum and the sum of the squares for the first 100 natural numbers. The solution is registered using the common::problem! macro, which integrates it into the project's common execution framework.

    // The solver logic is encapsulated in the solve() function
    // and registered via the common::problem! macro.
    common::problem!("25164150", solve);
  9. Solve Project Euler Problem 135

    master

    This binary is a solver for Project Euler Problem 135. It calculates the number of ways to represent an integer $n$ as the product of three terms in an arithmetic progression ($x^2 - y^2 = z^2$), specifically counting how many $n < 1,000,000$ have exactly 10 such representations.

    Note: This is a compiled Rust binary within the ProjectEulerRust workspace. It is invoked via cargo run --bin p135.

    cargo run --bin p135
  10. Run the Project Euler Problem 13 solver

    master

    The p013.rs file is a standalone binary for solving Project Euler Problem 13. It uses the common::problem! macro to register the solution with the project's runner. To execute this specific solver, you would typically use cargo run --bin p013 within the project directory.

    Note: The solution logic is contained in the solve() function, which calls compute() to process the hardcoded INPUT string, extracting the first 12 digits of each line and summing them up.

    cargo run --bin p013
  11. Use the `problem!` macro for simple and file-based solvers

    master

    The problem! macro generates a main function and a test_solve test function.

    Simple Solver Syntax

    Use this when your solver only needs to compute a value.

    problem!($answer:expr, $solver:expr);
    • $answer: The expected result (e.g., "42").
    • $solver: A function with the signature fn() -> String.

    File-based Solver Syntax

    Use this when your solver requires an input file. The macro will download the file from https://projecteuler.net/project/resources/{file_name} into a ./.cache directory if it is not present.

    problem!($answer:expr, $file:expr, $solver:expr);
    • $answer: The expected result.
    • $file: The name of the resource file (e.g., "input_01.txt").
    • $solver: A function with the signature fn(std::fs::File) -> std::io::Result<String>.
    // Example of a simple solver implementation
    fn my_solver() -> String {
        "42".to_string()
    }
    
    problem!("42", my_solver);
  12. Enumerate element combinations with `CombinationOverlap`

    master

    The CombinationOverlap iterator enumerates all combinations of elements from a slice, allowing for elements to be repeated (combinations with replacement). The iteratee vector may contain duplicate elements.

    use iter::CombinationOverlap;
    let nums = &[1, 2, 3];
    let mut it = CombinationOverlap::new(nums, 2);
    assert_eq!(Some(vec![1, 1]), it.next());
    assert_eq!(Some(vec![1, 2]), it.next());
    assert_eq!(Some(vec![1, 3]), it.next());
    assert_eq!(Some(vec![2, 2]), it.next());
    assert_eq!(Some(vec![2, 3]), it.next());
    assert_eq!(Some(vec![3, 3]), it.next());
    assert_eq!(None, it.next());