We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #10: Summation of primes
Project Euler #10: Summation of primes
Sort by
recency
|
216 Discussions
|
Please Login in order to post a comment
Did it with DIctionary Generated primes initially and added their sum.
t = int(input().strip()) for a0 in range(t): n = int(input().strip()) l=[2] for i in range(3,n+1,2): r=1 for j in l: if i%j==0: r=0 break if r: l.append(i)
print(sum(l))
its stilll showing timeout, how can i optimize it further??
My script in python was as follows:
It passed all tests. I implemented an equivalent approach in Rust but received timeout errors for 6 and 7. Any ideas?
NOTE: here is my rust code. `* ---------------------------------------------------------------- * * IMPORTS * ---------------------------------------------------------------- */
use std::io; use std::io::BufRead; use std::collections::HashMap;
/* ---------------------------------------------------------------- * * MAIN * ---------------------------------------------------------------- */
fn main() { let stdin = io::stdin(); let mut stdin_iterator = stdin.lock().lines();
}
/* ---------------------------------------------------------------- * * HELPER METHODS * ---------------------------------------------------------------- */
fn get_primes(t: i32) -> Vec { let mut map = HashMap::::new(); let mut result = Vec::::new(); for p in 2..=t { if map.get(&p) != Some(&false) { result.push(p); (2*p..=t).step_by(p as usize).for_each(|k| { map.entry(k).or_insert(false); }); } } return result; }
fn compute_aggregates( numbers: &Vec, n_max: i32, primes: &Vec, ) -> HashMap:: { let mut primes_ = primes.clone(); primes_.push(n_max + 1); let mut sum: i32 = 0; let mut sums = HashMap::::new(); let mut numbers_sorted = numbers.clone(); numbers_sorted.sort();
} 2 Min.
Haskell
include
include
include
using namespace std; bool isprime(long number){ if((number != 2 && number != 3 && number != 5) && (number % 2 == 0 || number % 3 == 0 || number % 5==0))
}
int main(){ vector primes; for(long i = 2; i<1000000; i++){ if (isprime(i)) primes.push_back(i); }
}