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

Published on 7 June 2024 03:52 AM
#C

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

Table of Contents

Problem Statement

All untouchable numbers   >  5  are composite numbers. No untouchable number is perfect. No untouchable number is sociable. No untouchable number is a Mersenne prime. No untouchable number is   one more   than a prime number,   since if   p   is prime,   then the sum of the proper divisors of   p2   is  p + 1. No untouchable number is   three more   than an odd prime number,   since if   p   is an odd prime,   then the sum of the proper divisors of   2p   is  p + 3. The number  5  is believed to be the only odd untouchable number,   but this has not been proven:   it would follow from a slightly stronger version of the   Goldbach's conjecture,   since the sum of the proper divisors of   pq   (with   p, q   being distinct primes)   is   1 + p + q. There are infinitely many untouchable numbers,   a fact that was proven by   Paul Erdős. According to Chen & Zhao,   their natural density is at least   d > 0.06.

Let's start with the solution:

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

This C program generates and analyzes a list of "untouchable" numbers within a specified limit. An untouchable number is an even integer that is abundant (the sum of its proper divisors is greater than itself) and has two prime numbers immediately preceding it (n-1 and n-3 are prime).

Key Concepts:

  • Prime Sieve: The primeSieve() function uses the Sieve of Eratosthenes algorithm to create an array c marking composite numbers (true) and prime numbers (false) up to the specified limit.

  • Sum of Divisors Array: The sumDivs array is populated with the sum of all positive divisors of each number up to the limit. This is used to identify abundant numbers.

  • Untouchable Numbers: An array s is used to mark abundant numbers as true. The untouchable array is then filled with even numbers that are abundant (as marked in s) and have two preceding primes (according to the c array).

Main Function:

  • Defines constants and initializes necessary arrays.
  • Calls primeSieve() to generate the prime sieve c.
  • Calculates the sum of divisors for numbers up to the limit and identifies abundant numbers.
  • Generates and stores untouchable numbers in the untouchable array.

Output:

  • Prints the first 2,000 untouchable numbers.
  • Reports the total number of untouchable numbers found within different ranges (e.g., <= 10,000, <= 100,000, etc.).

Overall:

The program efficiently identifies and analyzes untouchable numbers by combining the Sieve of Eratosthenes for prime identification, calculation of sum of divisors, and identification of abundant numbers. It provides detailed output with statistics on the number of untouchable numbers found within specified ranges.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>

bool *primeSieve(int limit) {
    int i, p;
    limit++;
    // True denotes composite, false denotes prime.
    bool *c = calloc(limit, sizeof(bool)); // all false by default
    c[0] = true;
    c[1] = true;
    for (i = 4; i < limit; i += 2) c[i] = true;
    p = 3; // Start from 3.
    while (true) {
        int p2 = p * p;
        if (p2 >= limit) break;
        for (i = p2; i < limit; i += 2 * p) c[i] = true;
        while (true) {
            p += 2;
            if (!c[p]) break;
        }
    }
    return c;
}

int main() {
    const int limit = 1000000;
    int i, j, n, uc = 2, p = 10, m = 63, ul = 151000;
    bool *c = primeSieve(limit);
    n = m * limit + 1;
    int *sumDivs = (int *)calloc(n, sizeof(int));
    for (i = 1; i < n; ++i) {
        for (j = i; j < n; j += i) sumDivs[j] += i;
    }
    bool *s = (bool *)calloc(n, sizeof(bool)); // all false
    for (i = 1; i < n; ++i) {
        int sum = sumDivs[i] - i; // proper divs sum
        if (sum <= n) s[sum] = true;
    } 
    free(sumDivs);
    int *untouchable = (int *)malloc(ul * sizeof(int));
    untouchable[0] = 2;
    untouchable[1] = 5;
    for (n = 6; n <= limit; n += 2) {
        if (!s[n] && c[n-1] && c[n-3]) untouchable[uc++] = n;
    }
    setlocale(LC_NUMERIC, "");
    printf("List of untouchable numbers <= 2,000:\n");
    for (i = 0; i < uc; ++i) {
        j = untouchable[i];
        if (j > 2000) break;
        printf("%'6d ", j);
        if (!((i+1) % 10)) printf("\n");
    }
    printf("\n\n%'7d untouchable numbers were found  <=     2,000\n", i);
    for (i = 0; i < uc; ++i) {
        j = untouchable[i];
        if (j > p) {
            printf("%'7d untouchable numbers were found  <= %'9d\n", i, p);
            p *= 10;
            if (p == limit) break;
        }
    }
    printf("%'7d untouchable numbers were found  <= %'d\n", uc, limit);
    free(c);
    free(s);
    free(untouchable);
    return 0;
}


  

You may also check:How to resolve the algorithm Untouchable numbers step by step in the C programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the C programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the C programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the C programming language
You may also check:How to resolve the algorithm Brace expansion step by step in the C programming language