You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

3.3 KiB

Pi Finder Library

A Zig library for searching numeric sequences in the decimal places of Pi.

Features

  • Efficient searching of numeric sequences in Pi decimal places
  • JSON output as standard
  • Optional plain text output format
  • Configurable context display with delimiters
  • Reusable module design
  • Performance metrics

Installation

As a Dependency in a Zig Project

Add this library as a dependency in your build.zig.zon:

.dependencies = .{
    .@"pi-finder" = .{
        .path = "path/to/pi-finder-lib",
    },
},

In your build.zig

const pi_finder_dep = b.dependency("pi-finder", .{
    .target = target,
    .optimize = optimize,
});

exe.root_module.addImport("pi-finder", pi_finder_dep.module("pi-finder"));

Usage

Basic Usage

const std = @import("std");
const PiFinder = @import("pi-finder");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // Initialize Pi Finder
    var pi_finder = try PiFinder.PiFinder.init(allocator, "pi-data.txt");
    defer pi_finder.deinit();

    // Configure context
    const context_config = PiFinder.ContextConfig{
        .size = 50,
        .position = 25,
        .delimiter = "-",
        .delimiter_interval = 2,
    };

    // Search for sequence
    const result = try pi_finder.searchSequence("123456", context_config, allocator);
    
    // Output as JSON (default format)
    const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
    defer allocator.free(json_output);
    
    std.debug.print("{s}\n", .{json_output});
}

Using Plain Text Output Format

// After getting the search result
const plain_text_output = try PiFinder.formatSearchResultAsPlainText(allocator, result);
defer allocator.free(plain_text_output);

std.debug.print("{s}\n", .{plain_text_output});

API

PiFinder

  • init(allocator, file_path) - Initializes the Pi Finder
  • deinit() - Frees allocated memory
  • findSequenceInPi(sequence) - Searches for a sequence
  • getContext(position, config, sequence, allocator) - Generates context
  • searchSequence(sequence, config, allocator) - Comprehensive search with structured result

Output Format Functions

  • formatSearchResultAsJson(allocator, result) - Formats search result as JSON
  • formatSearchResultAsPlainText(allocator, result) - Formats search result as plain text

ContextConfig

Configuration for context display:

  • size - Size of the context
  • position - Position of the sequence within the context
  • delimiter - Delimiter character (optional)
  • delimiter_interval - Interval for delimiter insertion
  • prefix - Prefix for the sequence
  • suffix - Suffix for the sequence

SearchResult

Structured result containing:

  • success - Search success status
  • sequence - Searched sequence
  • position - Position in Pi (if found)
  • context - Context around the position
  • error_message - Error message (if any)
  • performance - Performance metrics
  • context_config - Used context configuration

Examples

See the examples/ directory for complete examples:

  • birth-date-finder/ - Basic example for searching birth dates
  • birth-date-finder/src/plain_text_example.zig - Example using plain text output format