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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest proper divisor of n step by step in the Rust 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 Rust programming language

Source code in the rust programming language

fn largest_proper_divisor(n: i32) -> i32 {
    for i in 2..=(n as f64).sqrt() as i32 {
        if n % i == 0 {
            return n / i;
        }
    }
}

fn main() {
    println!("The largest proper divisors for numbers in the interval [1, 100] are:");
    print!(" 1  ");
    for n in 2..=100 {
        if n % 2 == 0 {
            print!("{:2}  ", n / 2);
        } else {
            print!("{:2}  ", largest_proper_divisor(n));
        }
        if n % 10 == 0 {
            println!();
        }
    }
}


fn lpd(n: u32) -> u32 {
    if n <= 1 {
        1
    } else {
        (1..n).rev().find(|&i| n % i == 0).unwrap_or(1)
    }
}

fn main() {
    for i in 1..=100 {
        print!("{:3}", lpd(i));

        if i % 10 == 0 {
            println!();
        }
    }
}


  

You may also check:How to resolve the algorithm Old Russian measure of length step by step in the BASIC programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm N'th step by step in the sed programming language
You may also check:How to resolve the algorithm Joystick position step by step in the C programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Java programming language