---
title: "Write a Rust program to calculate the sum of all prime numbers up to a given limit."  
description: "Write a Rust program to calculate the sum of all prime numbers up to a given limit."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158832/write-a-rust-program-to-calculate-the-sum-of-all-prime-numbers-up-to-a-given-limit  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust program to calculate the sum of all prime numbers up to a given limit.

Write a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program to calculate](https://www.mindstick.com/forum/158772/write-a-python-program-to-calculate-the-sum-of-digits-in-a-given-number) the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of all [prime numbers up](https://www.mindstick.com/forum/161532/generate-a-list-of-all-even-odd-and-prime-numbers-up-to-n-in-python-with-explanation) to a given limit.

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to calculate the sum of all prime numbers up to a given limit:

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 sum_of_primes(limit: i32) -> i32 {
    let mut sum = 0;
    for i in 2..limit + 1 {
        if is_prime(i) {
            sum += i;
        }
    }
    return sum;
}

fn main() {
    let limit = 100;
    let sum_of_primes = sum_of_primes(limit);
    println!("The sum of all prime numbers up to {} is {}", limit, sum_of_primes);
}
```

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, sum_of_primes, takes an i32 number as input and returns the sum of all prime numbers up to the number. The function first initializes a variable called sum to 0. Then, the function iterates through all the numbers from 2 to the number. If the number is prime, then the function adds the number to sum. Finally, the function returns sum.

The main function of the program then calls the sum_of_primes function with the value 100 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 sum_of_primes.rs and then compile it using the following command:

Code snippet

```plaintext
rustc sum_of_primes.rs
```

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

Code snippet

```plaintext
./sum_of_primes
```

This will print the following output to the console:

Code snippet

```plaintext
The sum of all prime numbers up to 100 is 1060
```


---

Original Source: https://www.mindstick.com/forum/158832/write-a-rust-program-to-calculate-the-sum-of-all-prime-numbers-up-to-a-given-limit

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
