Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rand;
- use rand::prelude::SliceRandom;
- const ROWS :[u32;3] = [165, 48,728];
- const COLS :[u32;3] = [462,160,78];
- fn factors(n: &u32) -> Vec<u32>{
- // Returns vec with factors of n
- let mut output : Vec<u32> = Vec::new();
- for i in 1..n+1 {
- if n % i == 0 {
- output.push(i);
- }
- }
- output
- }
- fn find_factors(row: usize, col: usize)->Vec<u32>{
- //returns vec with common factors to row and col
- let mut output = Vec::new();
- let afacts : Vec<u32> = factors(&ROWS[row]);
- let bfacts : Vec<u32> = factors(&COLS[col]);
- for a in afacts.iter(){
- if bfacts.contains(a) {
- output.push(*a)
- }
- }
- output
- }
- fn main() {
- let mut rnd = rand::thread_rng();
- let mut facts : Vec<Vec<Vec<u32>>> = vec![vec![vec![];3];3];
- for i in 0..3 {
- for j in 0..3 {
- facts[i][j] = find_factors(i, j);
- }
- }
- 'outside: loop {
- let mut val : Vec<Vec<u32>> = vec![vec![0;3];3]; //bit odd having to put ref to 0 !
- for i in 0..3 {
- for j in 0..3 {
- val[i][j] = *facts[i][j].choose(&mut rnd).unwrap();
- }
- }
- for i in 0..3 {
- if val[i][0] * val[i][1] * val[i][2] != ROWS[i] ||
- val[0][i] * val[1][i] * val[2][i] != COLS[i] {
- continue 'outside;
- }
- }
- println!(" {:?} {:?} {:?}", val[0][0], val[0][1], val[0][2]);
- println!(" {:?} {:?} {:?}", val[1][0], val[1][1], val[1][2]);
- println!(" {:?} {:?} {:?}", val[2][0], val[2][1], val[2][2]);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement