Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

Ways to make a sum of squares calculation

// Ways to make a sum of squares calculation

// Using an array passed to a function
fn sum_squares1(aa: [i32; 10] ) -> i32 {
    aa.iter().map(|x| (x * x)).sum::<i32>()
}

fn main() {
    let aa: [i32; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    println!("Sum of squares array = {} ", sum_squares1(aa) );  

    // Generating a sequence of numbers    
    let n = 10.0;
    let result1 = (1..=n as i32).map(|x| (x * x)).sum::<i32>();
    println!("Sum of squares sequence = {:?} ", result1); 

    // Making a calculation with no iterations
    let result2 = n * (n + 0.5) * (n + 1.0) / 3.0;   
    println!("Sum of squares calculation = {} ", result2);       
}
 
PREVIOUS NEXT
Tagged: #Ways #sum #squares #calculation
ADD COMMENT
Topic
Name
3+9 =