How to resolve the algorithm Jacobsthal numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Jacobsthal numbers step by step in the C programming language

Table of Contents

Problem Statement

Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1. Terms may be calculated directly using one of several possible formulas:

Jacobsthal-Lucas numbers are very similar. They have the same recurrence relationship, the only difference is an initial starting value J0 = 2 rather than J0 = 0. Terms may be calculated directly using one of several possible formulas: Jacobsthal oblong numbers is the sequence obtained from multiplying each Jacobsthal number Jn by its direct successor Jn+1.

Jacobsthal primes are Jacobsthal numbers that are prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jacobsthal numbers step by step in the C programming language

The provided C code uses the GMP library to work with large integer numbers and calculates and prints the Jacobsthal, Jacobsthal-Lucas, Jacobsthal oblong numbers, and Jacobsthal primes. Here's a detailed explanation of the code:

  • Header Includes:

    • The code includes the necessary headers for input/output (stdio.h) and the GMP library (gmp.h).
  • Function Declarations:

    • jacobsthal(mpz_t r, unsigned long n): Calculates the nth Jacobsthal number and stores the result in r.
    • jacobsthal_lucas(mpz_t r, unsigned long n): Calculates the nth Jacobsthal-Lucas number and stores the result in r.
  • Main Function:

    • Jacobsthal Numbers:

      • Initializes an array jac of 30 mpz_t variables to store the first 30 Jacobsthal numbers.
      • Iterates through the first 30 positive integers, computes each Jacobsthal number using jacobsthal, and prints it.
    • Jacobsthal-Lucas Numbers:

      • Initializes an mpz_t variable j to store the Jacobsthal-Lucas numbers.
      • Iterates through the first 30 positive integers, computes each Jacobsthal-Lucas number using jacobsthal_lucas, and prints it.
    • Jacobsthal Oblong Numbers:

      • Iterates through the first 20 positive integers, computes the product of successive Jacobsthal numbers (jac[i] * jac[i+1]), and prints it.
    • Jacobsthal Primes:

      • Iterates through positive integers until 20 Jacobsthal primes are found.
      • For each integer i, computes the Jacobsthal number j and checks if it's a probable prime using mpz_probab_prime_p. If it is, j is printed as a Jacobsthal prime.

Source code in the c programming language

#include <stdio.h>
#include <gmp.h>

void jacobsthal(mpz_t r, unsigned long n) {
    mpz_t s;
    mpz_init(s);
    mpz_set_ui(r, 1);
    mpz_mul_2exp(r, r, n);
    mpz_set_ui(s, 1);
    if (n % 2) mpz_neg(s, s);
    mpz_sub(r, r, s);
    mpz_div_ui(r, r, 3);
}

void jacobsthal_lucas(mpz_t r, unsigned long n) {
    mpz_t a;
    mpz_init(a);
    mpz_set_ui(r, 1);
    mpz_mul_2exp(r, r, n);
    mpz_set_ui(a, 1);
    if (n % 2) mpz_neg(a, a);
    mpz_add(r, r, a);
}

int main() {
    int i, count;
    mpz_t jac[30], j;
    printf("First 30 Jacobsthal numbers:\n");
    for (i = 0; i < 30; ++i) {
        mpz_init(jac[i]);
        jacobsthal(jac[i], i);
        gmp_printf("%9Zd ", jac[i]);
        if (!((i+1)%5)) printf("\n");
    }

    printf("\nFirst 30 Jacobsthal-Lucas numbers:\n");
    mpz_init(j);
    for (i = 0; i < 30; ++i) {
        jacobsthal_lucas(j, i);
        gmp_printf("%9Zd ", j);
        if (!((i+1)%5)) printf("\n");
    }

    printf("\nFirst 20 Jacobsthal oblong numbers:\n");
    for (i = 0; i < 20; ++i) {
        mpz_mul(j, jac[i], jac[i+1]);
        gmp_printf("%11Zd ", j);
        if (!((i+1)%5)) printf("\n");
    }

    printf("\nFirst 20 Jacobsthal primes:\n");
    for (i = 0, count = 0; count < 20; ++i) {
        jacobsthal(j, i);
        if (mpz_probab_prime_p(j, 15) > 0) {
            gmp_printf("%Zd\n", j);
            ++count;
        }
    }

    return 0;
}


  

You may also check:How to resolve the algorithm Quine step by step in the Phix programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Clojure programming language
You may also check:How to resolve the algorithm System time step by step in the Forth programming language
You may also check:How to resolve the algorithm Go Fish step by step in the Python programming language
You may also check:How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the J programming language