Skip to content

IntNextPrime

Description

Find the next probable prime greater than or equal to a value.

Parameters

Name Direction Description
result out Destination for the prime
value in Starting point

Usage example (from documentation)

  bool ok = IntNextPrime(&prime, &value);

Success

Returns true. *result holds the smallest probable prime >= *value.

Failure

Returns false on allocator OOM during the witness loop, or if the primality oracle gives up. *result is left untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool IntNextPrime(Int *result, const Int *value) {
        bool error = false;
    
    bool test_int_next_prime(void) {
        WriteFmt("Testing IntNextPrime\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str text  = StrInit(&alloc.base);
    
        bool ok = IntNextPrime(&next, &value);
        text    = IntToStr(&next);
    ///
    bool test_m7_next_prime_of_zero_is_two(void) {
        WriteFmt("Testing IntNextPrime(0) == 2\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int next  = IntInit(&alloc.base);
    
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 2);
    ///
    bool test_m7_next_prime_of_ten_is_eleven(void) {
        WriteFmt("Testing IntNextPrime(10) == 11\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int next  = IntInit(&alloc.base);
    
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 11);
    ///
    bool test_m7_next_prime_of_nine_is_eleven(void) {
        WriteFmt("Testing IntNextPrime(9) == 11\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int next  = IntInit(&alloc.base);
    
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 11);
    ///
    bool test_m7_next_prime_null_result(void) {
        WriteFmt("Testing IntNextPrime(NULL result) aborts\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(10, &alloc.base);
    
        (void)IntNextPrime(NULL, &value);
    
        IntDeinit(&value);
        Int r = IntFrom(7u, a);
    
        bool ok = IntNextPrime(&r, &v);
        ok      = ok && IntToU64(&r) == 1000003u;
        Int r1 = IntFrom(7u, a);
    
        bool ok = IntNextPrime(&r0, &v0) && IntToU64(&r0) == 2u;
        ok      = ok && IntNextPrime(&r1, &v1) && IntToU64(&r1) == 3u;
    
        bool ok = IntNextPrime(&r0, &v0) && IntToU64(&r0) == 2u;
        ok      = ok && IntNextPrime(&r1, &v1) && IntToU64(&r1) == 3u;
    
        IntDeinit(&v0);
        Int next  = IntFrom(0u, alloc);
    
        bool ok = IntNextPrime(&next, &value);
        ok      = ok && IntCompare(&next, 101u) == 0;
Last updated on