---
title: "Create a Rust program to find the largest prime factor of a given number."  
description: "Create a Rust program to find the largest prime factor of a given number."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158829/create-a-rust-program-to-find-the-largest-prime-factor-of-a-given-number  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Create a Rust program to find the largest prime factor of a given number.

Create a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to find the [largest prime](https://www.mindstick.com/forum/158743/write-a-program-to-find-the-largest-prime-number-within-a-given-range) factor of a given number.

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to find the largest prime factor of a given number:

Rust

```plaintext
fn is_prime(number: i32) -> bool {
    if number <= 1 {
        return false;
    }
    for i in 2..number {
        if number % i == 0 {
            return false;
        }
    }
    return true;
}

fn find_largest_prime_factor(number: i32) -> i32 {
    let mut largest_prime_factor = 1;
    for i in 2..number {
        if number % i == 0 && is_prime(i) {
            largest_prime_factor = i;
        }
    }
    return largest_prime_factor;
}

fn main() {
    let number = 600851475143;
    let largest_prime_factor = find_largest_prime_factor(number);
    println!("The largest prime factor of {} is {}", number, largest_prime_factor);
}
```

This program first defines a function called is_prime that takes an i32 number as input and returns a bool value. The function checks if the number is prime by iterating through all the numbers from 2 to the number. If the number is divisible by any of the numbers from 2 to the number, then the function returns false. Otherwise, the function returns true.

The second function, find_largest_prime_factor, takes an i32 number as input and returns the largest prime factor of the number. The function first initializes a variable called largest_prime_factor to 1. Then, the function iterates through all the numbers from 2 to the number. If the number is divisible by any of the numbers from 2 to the number and is also prime, then the function sets largest_prime_factor to the number. Finally, the function returns largest_prime_factor.

The main function of the program then calls the find_largest_prime_factor function with the value 600851475143 as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called find_largest_prime_factor.rs and then compile it using the following command:

Code snippet

```plaintext
rustc find_largest_prime_factor.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./find_largest_prime_factor
```

This will print the following output to the console:

Code snippet

```plaintext
The largest prime factor of 600851475143 is 6857
```


---

Original Source: https://www.mindstick.com/forum/158829/create-a-rust-program-to-find-the-largest-prime-factor-of-a-given-number

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
