Search
 
SCRIPT & CODE EXAMPLE
 

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);       
}
Comment

PREVIOUS NEXT
Code Example
Rust :: does rust support classes 
Rust :: rust language 
Rust :: rust•armanriazi•concept•semantic 
Rust :: armanriazi•rust•error•[E0507]: cannot move out of `self.step` which is behind a mutable reference self.curr += self.step; 
Rust :: rust from floating point to money 
Rust :: armanriazi•rust•error•cannot use the `?` operator in a function that returns `()` 
Rust :: rust convert floating point number, to a string, with decimal digits. 
Rust :: armanriazi•rust•collection•hashmap•avoid_of_duplicate 
Rust :: rust create derive trait 
Rust :: rust•armanriazi•error•value used here after move 
Rust :: rust•armanriazi•type•wraper 
Rust :: armanriazi•rust•code•string•to•u128 
Rust :: armanriazi•rust•concept•unrolling 
Rust :: rust how to make print happen before asking for input 
Rust :: split text on multiple separators and put into a list 
Lua :: how to save to a file lua 
Lua :: luau how to make region3 
Lua :: roblox debounce 
Lua :: lua float to int 
Lua :: lua click button 
Lua :: Startswith function in lua 
Lua :: lua indexof 
Lua :: lua convert function to string 
Lua :: lua hash table length 
Lua :: when do true loop on roblox lua 
Lua :: the function returning the address of a local variable results in: 
Lua :: how to detect collision in roblox studio 
Matlab :: pyspark dense 
Matlab :: z-score normalize values in tsv file matlab 
Basic :: split to arraylist vb.net 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =