How to resolve the algorithm Aliquot sequence classifications step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Aliquot sequence classifications step by step in the C programming language

Table of Contents

Problem Statement

An aliquot sequence of a positive integer K is defined recursively as the first member being K and subsequent members being the sum of the Proper divisors of the previous term.

Show all output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Aliquot sequence classifications step by step in the C programming language

This C program is designed to classify a given integer based on its aliquot sum behavior.

Aliquot Sum

An aliquot sum is the sum of all proper divisors of a number. For example, the aliquot sum of 12 is 28 (1 + 2 + 3 + 4 + 6 = 28).

Aliquot Classification

Integers are classified based on their aliquot sum behavior:

  • Terminating: Aliquot sum leads to 0 (e.g., 12)
  • Perfect: Aliquot sum is equal to the number itself (e.g., 6)
  • Amicable: Aliquot sum of one number is equal to another number, and vice versa (e.g., 220 and 284)
  • Aspiring: Aliquot sum of a number is equal to itself, but it takes more than 2 steps to reach it (e.g., 15)
  • Sociable: Aliquot sum of a number leads to another number, which in turn leads back to the original number (e.g., 12496)
  • Cyclic: Aliquot sum of a number leads to a sequence of numbers that repeat indefinitely (e.g., 153)
  • Non-Terminating: Aliquot sum does not fit any of the above categories and continues indefinitely (e.g., 87)

Brute-Force Approach

The program initially uses a brute-force approach (bruteForceProperDivisorSum) to calculate the aliquot sum of a number. This involves iteratively checking all numbers from 1 to n/2 to see if they divide n without a remainder.

Improved Approach

Later, it switches to an improved algorithm (properDivisorSum) to calculate the aliquot sum. This algorithm uses prime factorization and mathematical properties to efficiently determine the aliquot sum.

Main Function

The main function:

  • Parses command-line arguments.
  • If a filename is provided, it processes the file line by line.
  • Otherwise, it classifies the input integer.

Classification Process

The aliquotClassifier function:

  • Initializes an array to store the sequence of aliquot sums.
  • Repeatedly calculates the next aliquot sum and checks for termination conditions.
  • Classifies the integer based on the observed behavior.

Printing Results

The printSeries function prints the sequence of aliquot sums along with the classification type.

File Processing

If a filename is provided, the processFile function opens the file, reads each line as an integer, and classifies each integer.

This C program is an "Aliquot Classifier" that takes a positive integer as input and classifies it into one of several types based on the sum of its proper divisors. A proper divisor of a number is a positive divisor of that number other than the number itself. A perfect number has a sum of proper divisors equal to itself. An amicable pair of numbers have a sum of proper divisors of each other. An aspiring number has a sum of proper divisors less than itself but equal to the sum of proper divisors of another number. A sociable number has a sum of proper divisors greater than itself but equal to the sum of proper divisors of another number. A terminating number has a sum of proper divisors that eventually reaches 1 after a finite number of steps. A non-terminating number has a sum of proper divisors that does not reach 1 after any finite number of steps. A cyclic number has a sum of proper divisors that repeats itself after a finite number of steps.

Function Definitions:

  • bruteForceProperDivisorSum: Computes the sum of proper divisors of a given number using a brute-force approach.
  • printSeries: Prints a series of numbers along with their integer, type, and a description of the type.
  • aliquotClassifier: Classifies a given number as perfect, amicable, aspiring, sociable, cyclic, terminating, or non-terminating based on the sum of its proper divisors.
  • processFile: Reads a file containing a list of positive integers and classifies each integer using the aliquotClassifier function.

Main Function:

  • The main function processes command-line arguments.
  • If an argument is provided and it contains a period ('.'), it is assumed to be the name of a file, and the processFile function is called to read and classify numbers from the file.
  • Otherwise, the provided argument is assumed to be a single positive integer, and the aliquotClassifier function is called to classify it.

Optimized Version: The optimized version uses a faster algorithm to compute the sum of proper divisors, which is based on factorization. It is more efficient than the brute-force approach used in the previous version. The function properDivisorSum is used for this purpose.

Source code in the c programming language

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

unsigned long long bruteForceProperDivisorSum(unsigned long long n){
	unsigned long long i,sum = 0;
	
	for(i=1;i<(n+1)/2;i++)
		if(n%i==0 && n!=i)
			sum += i;
		
	return sum;
}

void printSeries(unsigned long long* arr,int size,char* type){
	int i;
	
	printf("\nInteger : %llu, Type : %s, Series : ",arr[0],type);
	
	for(i=0;i<size-1;i++)
		printf("%llu, ",arr[i]);
	printf("%llu",arr[i]);
}

void aliquotClassifier(unsigned long long n){
	unsigned long long arr[16];
	int i,j;
	
	arr[0] = n;
	
	for(i=1;i<16;i++){
		arr[i] = bruteForceProperDivisorSum(arr[i-1]);
		
		if(arr[i]==0||arr[i]==n||(arr[i]==arr[i-1] && arr[i]!=n)){
			printSeries(arr,i+1,(arr[i]==0)?"Terminating":(arr[i]==n && i==1)?"Perfect":(arr[i]==n && i==2)?"Amicable":(arr[i]==arr[i-1] && arr[i]!=n)?"Aspiring":"Sociable");
			return;
		}
		
		for(j=1;j<i;j++){
			if(arr[j]==arr[i]){
				printSeries(arr,i+1,"Cyclic");
				return;
			}
		}
	}
	
	printSeries(arr,i+1,"Non-Terminating");
}

void processFile(char* fileName){
	FILE* fp = fopen(fileName,"r");
	char str[21];
	
	while(fgets(str,21,fp)!=NULL)
		aliquotClassifier(strtoull(str,(char**)NULL,10));
	
	fclose(fp);
}

int main(int argC,char* argV[])
{
    if(argC!=2)
		printf("Usage : %s <positive integer>",argV[0]);
	else{
		if(strchr(argV[1],'.')!=NULL)
			processFile(argV[1]);
		else
			aliquotClassifier(strtoull(argV[1],(char**)NULL,10));
	}
	return 0;
}


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

unsigned long long raiseTo(unsigned long long base, unsigned long long power){
    unsigned long long result = 1,i;
    for (i=0; i<power;i++) {
        result*=base;
    }
    return result;
}

unsigned long long properDivisorSum(unsigned long long n){
	unsigned long long prod = 1; 
	unsigned long long temp = n,i,count = 0;

	while(n%2 == 0){
		count++;
		n /= 2;
	}
	
	if(count!=0)
		prod *= (raiseTo(2,count + 1) - 1);

	for(i=3;i*i<=n;i+=2){
		count = 0;
		
		while(n%i == 0){
			count++;
			n /= i;
		}
		
		if(count==1)
			prod *= (i+1);
		else if(count > 1)
			prod *= ((raiseTo(i,count + 1) - 1)/(i-1));
	}
	
	if(n>2)
		prod *= (n+1);

	return prod - temp;
}

void printSeries(unsigned long long* arr,int size,char* type){
	int i;
	
	printf("\nInteger : %llu, Type : %s, Series : ",arr[0],type);
	
	for(i=0;i<size-1;i++)
		printf("%llu, ",arr[i]);
	printf("%llu",arr[i]);
}

void aliquotClassifier(unsigned long long n){
	unsigned long long arr[16];
	int i,j;
	
	arr[0] = n;
	
	for(i=1;i<16;i++){
		arr[i] = properDivisorSum(arr[i-1]);
		
		if(arr[i]==0||arr[i]==n||(arr[i]==arr[i-1] && arr[i]!=n)){
			printSeries(arr,i+1,(arr[i]==0)?"Terminating":(arr[i]==n && i==1)?"Perfect":(arr[i]==n && i==2)?"Amicable":(arr[i]==arr[i-1] && arr[i]!=n)?"Aspiring":"Sociable");
			return;
		}
		
		for(j=1;j<i;j++){
			if(arr[j]==arr[i]){
				printSeries(arr,i+1,"Cyclic");
				return;
			}
		}
	}
	
	printSeries(arr,i+1,"Non-Terminating");
}

void processFile(char* fileName){
	FILE* fp = fopen(fileName,"r");
	char str[21];
	
	while(fgets(str,21,fp)!=NULL)
		aliquotClassifier(strtoull(str,(char**)NULL,10));
	
	fclose(fp);
}

int main(int argC,char* argV[])
{
    if(argC!=2)
		printf("Usage : %s <positive integer>",argV[0]);
	else{
		if(strchr(argV[1],'.')!=NULL)
			processFile(argV[1]);
		else
			aliquotClassifier(strtoull(argV[1],(char**)NULL,10));
	}
	return 0;
}


  

You may also check:How to resolve the algorithm Magic squares of odd order step by step in the Perl programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Create a file step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Rosetta Code/Fix code tags step by step in the PureBasic programming language