How to resolve the algorithm Largest proper divisor of n step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Largest proper divisor of n step by step in the C programming language

Table of Contents

Problem Statement

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest proper divisor of n step by step in the C programming language

The provided C code demonstrates the lpd function, which calculates the largest proper divisor (LPD) of a given integer n. The LPD of a number n is the largest positive integer that divides n without leaving a remainder, except for n itself.

Here's a detailed explanation of the code:

  • Header Files:

    • The code includes the <stdio.h> header file, which provides the standard input/output functions like printf.
  • Function lpd:

    • The lpd function takes one unsigned integer n as input.
    • It first checks if n is less than or equal to 1. If it is, the function returns 1 because 1 is the largest proper divisor of 1 and itself.
    • If n is greater than 1, the function iterates through all the positive integers from n-1 down to 1 using a for loop.
    • Inside the loop, the function checks if n is divisible by the current value of i without leaving a remainder (i.e., n % i == 0). If n is divisible by i, the function returns i as the LPD because it's the largest proper divisor found so far.
    • If no proper divisor is found during the iteration, the function returns 1 (which is the LPD of a prime number).
  • Function main:

    • The main function is the entry point of the program.
    • It uses a for loop to iterate through integers from 1 to 100, calling the lpd function for each integer and printing the result.
    • The printf statement within the loop prints the LPD of the current integer, padded with three spaces using %3d.
    • Every ten LPDs printed on the same line, a newline character is added to separate the output into groups of ten.
  • Example Output:

    • When the program runs, it calculates and prints the LPDs for integers from 1 to 100. The output would look like this:
    1 1 1 1 1 1 1 1 1 1
    1 1 2 1 1 2 1 2 2 2
    1 2 3 1 1 2 1 4 2 4
    1 2 3 1 1 2 1 4 2 4
    5 5 3 5 1 4 5 5 4 5
    1 2 3 1 1 2 1 4 2 4
    5 5 3 5 1 4 5 5 4 5
    1 1 1 1 1 1 1 1 1 1
    1 1 2 1 1 2 1 2 2 2
    1 2 3 1 1 2 1 4 2 4
    
  • Implementation Details:

    • The lpd function has a time complexity of O(n), where n is the input number. It checks the divisibility for all the positive integers from n-1 down to 1, which takes linear time.
    • The main function has a time complexity of O(n), where n is the number of integers from 1 to 100. It calls the lpd function for each integer, which adds to the overall time complexity.

Source code in the c programming language

#include <stdio.h>

unsigned int lpd(unsigned int n) {
    if (n<=1) return 1;
    int i;
    for (i=n-1; i>0; i--)
        if (n%i == 0) return i;   
}

int main() {
    int i;
    for (i=1; i<=100; i++) {
        printf("%3d", lpd(i));
        if (i % 10 == 0) printf("\n");
    }
    return 0;
}


  

You may also check:How to resolve the algorithm Zumkeller numbers step by step in the D programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the SuperCollider programming language
You may also check:How to resolve the algorithm Gauss-Jordan matrix inversion step by step in the Racket programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the E programming language