Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use rand::prelude::*;
fn process_fat_accounts(accounts: &mut [f32], base: f32) -> f32 {
let mut sum = 0_f32;
for i in 0..accounts.len() {
let money = accounts[i];
if money > base {
let f = (money / base).powf(1.3);
if accounts[i] - f > base {
accounts[i] -= f;
sum += f;
}
}
}
sum
}
fn process_poor_accounts(accounts: &mut [f32], base: f32, benefits: f32) {
let small_amount_limit = |x: f32| -> f32 {
if x > 20_f32 {
20_f32
} else {
x
}
};
//let all_factors = accounts.into_iter().filter(|&s| *s < base).fold(0f32, |sum, val| sum + small_amount_limit(base / *val));
let mut all_factors = 0_f32;
//for mm in accounts {
for i in 0..accounts.len() {
let mm = accounts[i];
if mm < base {
all_factors += small_amount_limit(base / mm);
}
}
let amount_to_add = benefits / all_factors;
for i in 0..accounts.len() {
let money = accounts[i];
if money < base {
let f = small_amount_limit(base / money) * amount_to_add;
accounts[i] += f;
}
}
}
fn main() {
//let mut accounts: Vec<f32> = Vec::with_capacity(100000);
//let mut accounts = vec![0_f32; 10000];
let mut rng = thread_rng();
const N: usize = 1000_000;
let mut accounts = [0_f32; N];
for i in 0..N {
accounts[i] = rng.gen_range(10, 100_000) as f32;
}
let base: f32 = accounts.iter().fold(0f32, |sum, val| sum + val) / accounts.len() as f32;
println!("base: {:.2}", base);
for i in 0..100 {
let surplus = process_fat_accounts(&mut accounts, base);
process_poor_accounts(&mut accounts, base, surplus);
//println!("[ {}] счета {:.2?} снято с жирных счетов #{:.2}", i, accounts.iter().take(10), benefits);
println!("[ {}] снято с жирных счетов #{:.2}", i, surplus);
//accounts.iter().fold(String::new(), |acc, &num| acc + &num.to_string() + ", ")
}
println!("processed:");
}
elapsed 0m3.478s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.