|
Sum of prime numbers
|
|
04-04-2010, 04:22 AM
Post: #1
|
|||
|
|||
|
Sum of prime numbers
I made a small example of sum of prime numbers.
More about prime numbers you have here : http://www.pro9ramming.com/program-for-f...-t-60.html So basically if you have sequence of primes : Code: 2,3,5,7,11 . . .So here is the code in Turbo Pascal for Windows : Code: program sum_of_prime_numbers;We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up. Bram Cohen - Bit Torrent Creator |
|||
|
04-04-2010, 12:17 PM
Post: #2
|
|||
|
|||
|
RE: Sum of prime numbers
Here is a link to sum of primes I did a while back http://www.pro9ramming.com/prime-sum-cha...t-796.html
|
|||
|
04-04-2010, 09:11 PM
Post: #3
|
|||
|
|||
RE: Sum of prime numbers
(04-04-2010 12:17 PM)codecaine Wrote: Here is a link to sum of primes I did a while back http://www.pro9ramming.com/prime-sum-cha...t-796.htmlYeah, that is below 5000 ! Very good ! We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up. Bram Cohen - Bit Torrent Creator |
|||
|
04-23-2010, 06:00 AM
Post: #4
|
|||
|
|||
|
RE: Sum of prime numbers
Here is the same code done in C:
Code: #include<stdio.h> |
|||
|
04-23-2010, 07:21 AM
Post: #5
|
|||
|
|||
|
RE: Sum of prime numbers
Very good TheBear. That is for validation if number is prime.
But few optimizations. When you look into definition of prime numbers on Wikipedia : Prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. As you can see primes are natural numbers that means that they can be described as unsigned integers. So instead of every "int" in program you could use "unsigned int". In function prime() you could declare it as short because if you declare it as int that is memory waste. Because bool is not defined in C (it is in C++) you need to use integer types, but memory is important and short uses less than int. Also in this part : Code: if (n==2)I like this line : Code: for(i=2; i<=floor((sqrt(n))+1); i++)But this line has one mistake. You already checked if 2 is divisor of that number, so loop starts with 3, like this : Code: for(i=3; i<=floor((sqrt(n)))+1; i++)All in all, it's okay, just a few corrections. We respect work ! We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up. Bram Cohen - Bit Torrent Creator |
|||
|
04-23-2010, 07:59 PM
Post: #6
|
|||
|
|||
|
RE: Sum of prime numbers
Thank you for the corrections.
Optimization is something i have yet to learn. |
|||
|
04-23-2010, 10:02 PM
Post: #7
|
|||
|
|||
RE: Sum of prime numbers
(04-23-2010 07:59 PM)TheBear Wrote: Thank you for the corrections.Yeah that is positive. Basically, just think generally about the algorithm and try to find a way to optimize it by making loops doing smaller amount of iterations, or maybe calling some functions less. You can do it by using compiler's faster functions etc. If you find a way that is totally different than bruteforce, even optimized bruteforce, that can do things by certain formula or with very small number of iterations, that is dynamic programming. BTW in this line : Code: for(i=2; i<=floor((sqrt(n))+1); i++)So this line should look like this: Code: for(i=3; i<=floor(sqrt(n))+1; i+=2)We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up. Bram Cohen - Bit Torrent Creator |
|||
|
04-23-2010, 10:51 PM
Post: #8
|
|||
|
|||
|
RE: Sum of prime numbers
Even I know that after today's class
|
|||
|
« Next Oldest | Next Newest »
|





