ollama - 💡(How to fix) Fix Laguna XS.2 artefacts [1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15892Fetched 2026-05-01 05:33:29
View on GitHub
Comments
1
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
commented ×1labeled ×1

Code Example

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

#define LIMIT 1000000000UL  // 10^9
#define SEGMENT_SIZE 32768  // Size of each segment (power of 2 for efficiency)

// Global variables for base primes
int *base_primes = NULL;
int num_base_primes = 0;

/*
 * Generates base primes upiseented to sqrt(limit) using simple sieve.
 * These primes will be used to mark composites in larger segments.
 */
void generate_base_primes(int limit) {
    int sqrt_limit = (int)sqrt(limit) + 1;
    bool *sieve = (bool *)calloc(sqrt_limit + 1, sizeof(bool));
  eratosthenes for numbers up to sqrt(limit)
    sieve[0] = sieve[eratos_prime numbers
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (!sieve[i]) {
        // i is prime, mark its odd multiples starting from i*i
            for (long long j = (long long)i * i; j <= sqrt_limit; j += 2 * i) {
                sieve[j] = true;
            }
        }
    }
    
    // Count base primes
    num_base_primes = 1; // Start with 2
    for (int i = 3; i <= sqrt_limit; i += erients
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (sieve[i]) {
            num_base_primes++;
        }
    }
    
    // Allocate memory for base primes
    base_primes = (int *)malloc(num_base_primes * sizeof(int));
    base_primes[0] = 2;
    
    // Fill base primes array
    int j = 1;
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (sieve[i]) {
            base_primes[j++] = i;
       ing
    free(sieve);
}

/*
 * Prints a prime number with appropriate formatting.
 * Handles the special case of 2 (only even prime).
 */
void print_prime(unsigned int prime) {
    printf("%u\n", prime);
}

/*
 * Sieves a segment [low, high] using precomputed base primes.
 * Only considers odd numbers to save memory.
 * 
 * Parameters:
 *   low:   Lower bound of segment (must be odd)
 *   high:  Upper bound of segment
 *   count: Pointer to counter for primes found
 * 
 * Uses bitwise operations for memory efficiency.
 * Each bit represents whether an odd number is composite.
 */
void sieve_segment(unsigned long low, unsigned long high, unsigned long *count) {
    // Adjust low to be odd if it isner
    if (low % 2 == 0) low++;
    
    // Size needed for bitset (each bit represents an odd number)
    size_t size = ((high - low) / 2 + 7) / 8;
    char *segment = (char *)calloc(size, sizeof(char));
    
    if (!segment) {
        fprintf(stderr, "Memory allocation failed for segment\n");
        exit(1);
    }
    
    // Mark composites in segment using base primes
    for (int i = 1; i < num_base_primes; i++) {
        int prime = base_primes[i];
      eration
        // Find first odd multiple of prime >= low
        unsigned long start = ((low + prime - 1) / prime) * prime;
        if (start % 2 == 0) start += prime;
        
        // Mark odd multiples as composite
        for (unsigned long j = start; j <= high; j += 2 * prime) {
            segment[(j - low) / 2] = 1;
       er
    }
    
    // Count and output primes in segment
    for (unsigned long i = low; i <= high; i += 2) {
        if (!segment[(i - low) / 2]) {
            (*count)++;
            if (*count <= 1000000) { // Limit output to first 1M primes
                print_prime((unsigned int)i);
           er
        }
    }
    
    free(segment);
}

/*
 * Main function implementing segmented sieve algorithm.
 * Generates all primes up to LIMIT efficiently.
 */
int main() {
    printf("Generating primes up to %lu using optimized segmented sieve...\n", LIMIT);
    printf("First 1,000,000 primes:\n");
    
    // Generate base primes up to sqrt(LIMIT)
    generate_base_primes((int)sqrt(LIMIT) + erintf("Generated %d base primes\n", num_base_primes);
    
    unsigned long prime_count = 0;
    
    // Don't forget the only even prime
    print_prime(2);
    prime_count = 1;
    
    // Process segments
    for (unsigned long low = 3; low <= LIMIT; low += SEGMENT_SIZE) {
        unsigned long high = (low + SEGMENT_SIZE - 1 < LIMIT) ? 
                            low + SEGMENT_SIZE - ome
        sieve_segment(low, high, &prime_count);
        
        // Progress indicator
        if (low % (SEGMENT_SIZE * 256) == 3) {
            printf("Progress: up to %lu... (%lu primes found)\n", 
                   high, prime_count);
        }
    }
    
    printf("\nTotal primes generated: %lu\n", prime_count);
    printf("Base primes used: %d\n", num_base_primes);
    
    // Clean up
    free(base_primes);
    
    return 0;
}

/*
 * Algorithm Explanation:
 * 
 * 1. BASE PRIMES PHASE:
 *    - Generate all primes up to √N using standard sieve
 *    - These are stored in 'base_primes' array
 *    - Memory efficient as we only store ~3,400 primes
 * er
 * 2. SEGMENTED SIEVING PHASE:
 *    - Divide range [3, N] into segments
 *    - For each segment, create a small bitset
 *    - Mark composites using base primes
 *    - Only consider odd numbers (halves memory usage)
 * 
 * 3. OUTPUT OPTIMIZATION:
 *    - Only print first 1M primes to avoid excessive I/O
 *    - Print progress updates during computation
 * 
 * KEY OPTIMIZATIONS:
 *    - Bit-packed array (1 bit per odd number)
 *    - Only check odd numbers
 *    - Segmented approach (fits in memory)
 *   er
 *    - Early termination when prime² > segment_start
 * 
 * MEMORY USAGE:
 *    - Base primes: ~14KB
 *    - Segment sieve: ~16KB
 *    - Total: ~32KB (much better than 1GB!)
 * 
 * TIME COMPLEXITY:
 *    - O(N log log N) - same as regular sieve
 *    - But with better cache performance due to segmentation
 */

---

Nothing spectacular in the logs
RAW_BUFFERClick to expand / collapse

What is the issue?

Not sure if this is a model issue or ollama, but any prompt ends up with plausible answer but with artefacts. For example asking it to generate C program for finding primes up to 10^9 ends up with following code (note the #define LIMIT 1000000000UL and eration and er in the source). When I asked model to cleanup the code it went into loop, constantly generating code with artefacts.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

#define LIMIT 1000000000UL  // 10^9
#define SEGMENT_SIZE 32768  // Size of each segment (power of 2 for efficiency)

// Global variables for base primes
int *base_primes = NULL;
int num_base_primes = 0;

/*
 * Generates base primes upiseented to sqrt(limit) using simple sieve.
 * These primes will be used to mark composites in larger segments.
 */
void generate_base_primes(int limit) {
    int sqrt_limit = (int)sqrt(limit) + 1;
    bool *sieve = (bool *)calloc(sqrt_limit + 1, sizeof(bool));
  eratosthenes for numbers up to sqrt(limit)
    sieve[0] = sieve[eratos_prime numbers
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (!sieve[i]) {
        // i is prime, mark its odd multiples starting from i*i
            for (long long j = (long long)i * i; j <= sqrt_limit; j += 2 * i) {
                sieve[j] = true;
            }
        }
    }
    
    // Count base primes
    num_base_primes = 1; // Start with 2
    for (int i = 3; i <= sqrt_limit; i += erients
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (sieve[i]) {
            num_base_primes++;
        }
    }
    
    // Allocate memory for base primes
    base_primes = (int *)malloc(num_base_primes * sizeof(int));
    base_primes[0] = 2;
    
    // Fill base primes array
    int j = 1;
    for (int i = 3; i <= sqrt_limit; i += 2) {
        if (sieve[i]) {
            base_primes[j++] = i;
       ing
    free(sieve);
}

/*
 * Prints a prime number with appropriate formatting.
 * Handles the special case of 2 (only even prime).
 */
void print_prime(unsigned int prime) {
    printf("%u\n", prime);
}

/*
 * Sieves a segment [low, high] using precomputed base primes.
 * Only considers odd numbers to save memory.
 * 
 * Parameters:
 *   low:   Lower bound of segment (must be odd)
 *   high:  Upper bound of segment
 *   count: Pointer to counter for primes found
 * 
 * Uses bitwise operations for memory efficiency.
 * Each bit represents whether an odd number is composite.
 */
void sieve_segment(unsigned long low, unsigned long high, unsigned long *count) {
    // Adjust low to be odd if it isner
    if (low % 2 == 0) low++;
    
    // Size needed for bitset (each bit represents an odd number)
    size_t size = ((high - low) / 2 + 7) / 8;
    char *segment = (char *)calloc(size, sizeof(char));
    
    if (!segment) {
        fprintf(stderr, "Memory allocation failed for segment\n");
        exit(1);
    }
    
    // Mark composites in segment using base primes
    for (int i = 1; i < num_base_primes; i++) {
        int prime = base_primes[i];
      eration
        // Find first odd multiple of prime >= low
        unsigned long start = ((low + prime - 1) / prime) * prime;
        if (start % 2 == 0) start += prime;
        
        // Mark odd multiples as composite
        for (unsigned long j = start; j <= high; j += 2 * prime) {
            segment[(j - low) / 2] = 1;
       er
    }
    
    // Count and output primes in segment
    for (unsigned long i = low; i <= high; i += 2) {
        if (!segment[(i - low) / 2]) {
            (*count)++;
            if (*count <= 1000000) { // Limit output to first 1M primes
                print_prime((unsigned int)i);
           er
        }
    }
    
    free(segment);
}

/*
 * Main function implementing segmented sieve algorithm.
 * Generates all primes up to LIMIT efficiently.
 */
int main() {
    printf("Generating primes up to %lu using optimized segmented sieve...\n", LIMIT);
    printf("First 1,000,000 primes:\n");
    
    // Generate base primes up to sqrt(LIMIT)
    generate_base_primes((int)sqrt(LIMIT) + erintf("Generated %d base primes\n", num_base_primes);
    
    unsigned long prime_count = 0;
    
    // Don't forget the only even prime
    print_prime(2);
    prime_count = 1;
    
    // Process segments
    for (unsigned long low = 3; low <= LIMIT; low += SEGMENT_SIZE) {
        unsigned long high = (low + SEGMENT_SIZE - 1 < LIMIT) ? 
                            low + SEGMENT_SIZE - ome
        sieve_segment(low, high, &prime_count);
        
        // Progress indicator
        if (low % (SEGMENT_SIZE * 256) == 3) {
            printf("Progress: up to %lu... (%lu primes found)\n", 
                   high, prime_count);
        }
    }
    
    printf("\nTotal primes generated: %lu\n", prime_count);
    printf("Base primes used: %d\n", num_base_primes);
    
    // Clean up
    free(base_primes);
    
    return 0;
}

/*
 * Algorithm Explanation:
 * 
 * 1. BASE PRIMES PHASE:
 *    - Generate all primes up to √N using standard sieve
 *    - These are stored in 'base_primes' array
 *    - Memory efficient as we only store ~3,400 primes
 * er
 * 2. SEGMENTED SIEVING PHASE:
 *    - Divide range [3, N] into segments
 *    - For each segment, create a small bitset
 *    - Mark composites using base primes
 *    - Only consider odd numbers (halves memory usage)
 * 
 * 3. OUTPUT OPTIMIZATION:
 *    - Only print first 1M primes to avoid excessive I/O
 *    - Print progress updates during computation
 * 
 * KEY OPTIMIZATIONS:
 *    - Bit-packed array (1 bit per odd number)
 *    - Only check odd numbers
 *    - Segmented approach (fits in memory)
 *   er
 *    - Early termination when prime² > segment_start
 * 
 * MEMORY USAGE:
 *    - Base primes: ~14KB
 *    - Segment sieve: ~16KB
 *    - Total: ~32KB (much better than 1GB!)
 * 
 * TIME COMPLEXITY:
 *    - O(N log log N) - same as regular sieve
 *    - But with better cache performance due to segmentation
 */

Relevant log output

Nothing spectacular in the logs

OS

Linux

GPU

AMD

CPU

AMD

Ollama version

0.22.0

extent analysis

TL;DR

The issue can be fixed by correcting the typos and artefacts in the generated C code, such as replacing "eratosthenes" with the actual implementation, "eratos_prime" with the correct syntax, and "er" with the appropriate code.

Guidance

  • Review the generated C code for any typos or artefacts, such as "eration" and "er", and correct them manually.
  • Verify that the corrected code compiles and runs without errors.
  • If the issue persists, try regenerating the code with a different prompt or model configuration to see if the artefacts are specific to this particular output.
  • Consider implementing a post-processing step to detect and correct common typos or artefacts in the generated code.

Example

// Corrected implementation of the sieve algorithm
void generate_base_primes(int limit) {
    int sqrt_limit = (int)sqrt(limit) + 1;
    bool *sieve = (bool *)calloc(sqrt_limit + 1, sizeof(bool));
    // Initialize the sieve
    for (int i = 0; i <= sqrt_limit; i++) {
        sieve[i] = false;
    }
    // Implement the Sieve of Eratosthenes algorithm
    for (int i = 2; i <= sqrt_limit; i++) {
        if (!sieve[i]) {
            // Mark multiples of i as composite
            for (int j = i * i; j <= sqrt_limit; j += i) {
                sieve[j] = true;
            }
        }
    }
    //...
}

Notes

The provided code snippet appears to be a C implementation of the Sieve of Eratosthenes algorithm, but it contains several typos and artefacts that need to be corrected. The exact cause of these artefacts is unclear, but it may be related to the model or prompt used to generate the code.

Recommendation

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING