From 0b167e71f3679b80a5a76fc255e72100b789157d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 15 Jun 2020 14:18:14 -0400 Subject: [PATCH] Add getNumPrimes --- src/Algorithms.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 16 +++++++ 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/src/Algorithms.rs b/src/Algorithms.rs index 112a1bf..52639ee 100644 --- a/src/Algorithms.rs +++ b/src/Algorithms.rs @@ -1,7 +1,7 @@ extern crate num; -// +//This function returns a list of all Fibonacci numbers <= goalNumber pub fn getAllFib(goalNumber: u64) -> Vec{ let mut fibNums = Vec::new(); //A list to save the Fibonacci numbers //If the number is <= 0 return an empty list @@ -21,8 +21,6 @@ pub fn getAllFib(goalNumber: u64) -> Vec{ fibNums.remove(fibNums.len() - 1); return fibNums; } - -// pub fn getAllFibBig(goalNumber: num::BigInt) -> Vec{ let mut fibNums = Vec::new(); //A list to save the Fibonacci numbers in //If the number is <= 0 return an empty list @@ -42,7 +40,7 @@ pub fn getAllFibBig(goalNumber: num::BigInt) -> Vec{ return fibNums; } -//Ths function returns all factors of goalNumber +//This function returns all factors of goalNumber pub fn getFactors(mut goalNumber: i64) -> Vec{ //You need to get all the primes that could be factors of this number so you can test them let topPossiblePrime = (goalNumber as f64).sqrt().ceil() as i64; @@ -207,3 +205,101 @@ pub fn getPrimesBig(goalNumber: num::BigInt) -> Vec{ primes.sort(); return primes; } + +//This function gets a certain number of primes +pub fn getNumPrimes(numberOfPrimes: i64) -> Vec{ + let mut primes = Vec::::new(); //Holds the prime numbers + let mut foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the number is 0 or negative return an empty list + if(numberOfPrimes <= 0){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.push(2); + } + + //We can now start at 3 and skip all even numbers, because the cannot be prime + let mut possiblePrime = 3; + while((primes.len() as i64) < numberOfPrimes){ + //Check all current primes, up to sqrt)possiblePrime), to see if there is a divisor + let topPossibleFactor = (possiblePrime as f64).sqrt().ceil() as i64; + //We can safely assume that there will be at least 1 element in primes list because of 2 being added before this + let mut primesCnt = 0; + while(primes[primesCnt] <= topPossibleFactor){ + if((possiblePrime as i64 % primes[primesCnt]) == 0){ + foundFactor = true; + break; + } + else{ + primesCnt += 1; + } + //Check if the index has gone out of range + if(primesCnt >= primes.len()){ + break; + } + } + //If you didn't find a factor then the current number must be prime + if(!foundFactor){ + primes.push(possiblePrime as i64); + } + else{ + foundFactor = false; + } + possiblePrime += 2; + } + + //Sort the list before returning it + primes.sort(); + return primes; +} +pub fn getNumPrimesBig(numberOfPrimes: num::BigInt) -> Vec{ + let mut primes = Vec::::new(); //Holds the prime numbers + let mut foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the number is 0 or negative return an empty list + if(numberOfPrimes <= num::BigInt::from(1)){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.push(num::BigInt::from(2)); + } + + //We can now start at 3 and skip all even numbers, because they cannot be prime + let mut possiblePrime = num::BigInt::from(3); + while(numberOfPrimes > num::BigInt::from(primes.len())){ + //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor + let topPossibleFactor = ((&possiblePrime).sqrt() + num::BigInt::from(1)); + //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this + let mut primesCnt = 0; + while(primes[primesCnt] <= topPossibleFactor){ + if((&possiblePrime % &primes[primesCnt]) == num::BigInt::from(0)){ + foundFactor = true; + break; + } + else{ + primesCnt += 1; + } + //Check if the index has gone out of bounds + if(primesCnt >= primes.len()){ + break; + } + } + + //If you didn't find a factor then the current number must be prime + if(!foundFactor){ + primes.push(num::BigInt::new(possiblePrime.sign(), possiblePrime.to_u32_digits().1)); + } + else{ + foundFactor = false; + } + //Advance to the next number + possiblePrime += 2; + } + + //Sort the list before returning it + primes.sort(); + return primes; +} diff --git a/src/lib.rs b/src/lib.rs index 65f514c..9764734 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,22 @@ mod AlgorithmsTests{ let answer3 = super::Algorithms::getFactorsBig(number3); assert_eq!(correctAnswer3, answer3); } + #[test] + fn testGetNumPrimes(){ + //Test 1 + let mut correctAnswer1 = Vec::::new(); + correctAnswer1.extend_from_slice(&[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]); + let numPrimes1 = 25; + let answer1 = super::Algorithms::getNumPrimes(numPrimes1); + assert_eq!(correctAnswer1, answer1); + + //Test 2 + let mut correctAnswer2 = Vec::::new(); + correctAnswer2.extend_from_slice(&[num::BigInt::from(2), num::BigInt::from(3), num::BigInt::from(5), num::BigInt::from(7), num::BigInt::from(11), num::BigInt::from(13), num::BigInt::from(17), num::BigInt::from(19), num::BigInt::from(23), num::BigInt::from(29), num::BigInt::from(31), num::BigInt::from(37), num::BigInt::from(41), num::BigInt::from(43), num::BigInt::from(47), num::BigInt::from(53), num::BigInt::from(59), num::BigInt::from(61), num::BigInt::from(67), num::BigInt::from(71), num::BigInt::from(73), num::BigInt::from(79), num::BigInt::from(83), num::BigInt::from(89), num::BigInt::from(97)]); + let numPrimes2 = num::BigInt::from(25); + let answer2 = super::Algorithms::getNumPrimesBig(numPrimes2); + assert_eq!(correctAnswer2, answer2); + } }