How to resolve the algorithm Chowla numbers step by step in the C++ programming language
How to resolve the algorithm Chowla numbers step by step in the C++ programming language
Table of Contents
Problem Statement
Chowla numbers are also known as:
The chowla number of n is (as defined by Chowla's function):
The sequence is named after Sarvadaman D. S. Chowla, (22 October 1907 ──► 10 December 1995), a London born Indian American mathematician specializing in number theory.
German mathematician Carl Friedrich Gauss (1777─1855) said:
Chowla numbers can also be expressed as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chowla numbers step by step in the C++ programming language
This C++ code is designed to explore the fascinating topics of Chowla numbers, prime numbers, and perfect numbers. Here's a detailed breakdown:
Chowla Numbers:
- The
chowla
function calculates the sum of the proper divisors of a given numbern
. Proper divisors are positive divisors ofn
that are less thann
itself. - The function iterates through numbers from 2 to the square root of
n
, checking ifn
is divisible by them. - If
n
is divisible by a numberi
, the sum is incremented by bothi
andn/i
(if they are different).
Sieve of Eratosthenes:
- The
sieve
function uses the Sieve of Eratosthenes algorithm to efficiently find prime numbers. - It initializes a boolean vector
c
of sizelimit
tofalse
(assuming all numbers initially prime). - Starting from 3, it marks multiples of each prime as composite (true).
- The sieve ensures that only odd numbers are checked for primality (even numbers are composite except 2).
Prime Counting:
- The main function counts prime numbers within certain limits.
- It uses the
sieve
function to find primes up to 10 million and prints the count at various power-of-10 intervals.
Perfect Numbers:
- A number is perfect if the sum of its proper divisors (excluding itself) equals the number itself.
- The code iterates through pairs of consecutive odd numbers (called "Euler pairs") and checks if the product of the pair is a perfect number.
- It uses the
chowla
function to efficiently calculate the sum of proper divisors and count the number of perfect numbers up to a limit of 35 million.
In summary, this code explores the mathematical concepts of Chowla numbers, prime numbers, and perfect numbers, providing insights into their properties and generating counts within specified limits. It uses efficient algorithms like the Sieve of Eratosthenes for prime counting and the Chowla function for finding the sum of proper divisors.
Source code in the cpp programming language
#include <vector>
#include <iostream>
using namespace std;
int chowla(int n)
{
int sum = 0;
for (int i = 2, j; i * i <= n; i++)
if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
return sum;
}
vector<bool> sieve(int limit)
{
// True denotes composite, false denotes prime.
// Only interested in odd numbers >= 3
vector<bool> c(limit);
for (int i = 3; i * 3 < limit; i += 2)
if (!c[i] && (chowla(i) == 0))
for (int j = 3 * i; j < limit; j += 2 * i)
c[j] = true;
return c;
}
int main()
{
cout.imbue(locale(""));
for (int i = 1; i <= 37; i++)
cout << "chowla(" << i << ") = " << chowla(i) << "\n";
int count = 1, limit = (int)(1e7), power = 100;
vector<bool> c = sieve(limit);
for (int i = 3; i < limit; i += 2)
{
if (!c[i]) count++;
if (i == power - 1)
{
cout << "Count of primes up to " << power << " = "<< count <<"\n";
power *= 10;
}
}
count = 0; limit = 35000000;
int k = 2, kk = 3, p;
for (int i = 2; ; i++)
{
if ((p = k * kk) > limit) break;
if (chowla(p) == p - 1)
{
cout << p << " is a number that is perfect\n";
count++;
}
k = kk + 1; kk += k;
}
cout << "There are " << count << " perfect numbers <= 35,000,000\n";
return 0;
}
You may also check:How to resolve the algorithm Multiplication tables step by step in the BASIC programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Factor programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the J programming language
You may also check:How to resolve the algorithm Multisplit step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Literals/String step by step in the DWScript programming language