Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,635 members, 7,801,833 topics. Date: Friday, 19 April 2024 at 01:18 AM

Coding Challenge 3: Sum Of Primes - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Coding Challenge 3: Sum Of Primes (3903 Views)

Mini Web Application Coding Challenge For Programmers / Java Coding Challenge: Task Scheduler / Coding Challenge 5: Substring Generator (2) (3) (4)

(1) (2) (Reply) (Go Down)

Coding Challenge 3: Sum Of Primes by Fayimora(m): 10:04am On Jul 06, 2011
Challenge
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Your task is to find the sum of all the primes below two million.

Time limit: nil

Goodluck,
Re: Coding Challenge 3: Sum Of Primes by Nobody: 4:49pm On Jul 06, 2011
Re: Coding Challenge 3: Sum Of Primes by dellnet: 7:13pm On Jul 06, 2011
Little cheat.
#include <windows.h>
#include <stdio.h>
#include <math.h>
void main()
{
long sum, two_mil;
two_mil = 10; /*change to 2 mil*/
sum =0;
bool prime;
for (long x =2; x < two_mil; x++)
{
prime = false;
for (long p = 2; p < x; p++)
{
if (fmod((double)x / (double)p,1)==0 && (x !=p))
prime = true;
}
if(prime == false)
sum+=x;
}
printf("%d\n", sum);
system("pause"wink;

}
Re: Coding Challenge 3: Sum Of Primes by Nobody: 7:30pm On Jul 06, 2011
Re: Coding Challenge 3: Sum Of Primes by dellnet: 7:35pm On Jul 06, 2011
yes it takes forever, #1. Just comment the windows header file and the last line.
Re: Coding Challenge 3: Sum Of Primes by Nobody: 7:45pm On Jul 06, 2011
Re: Coding Challenge 3: Sum Of Primes by dellnet: 8:32pm On Jul 06, 2011
// Outputs 142913828922 to the standard output stream.

what do you mean?
Re: Coding Challenge 3: Sum Of Primes by Nobody: 8:54pm On Jul 06, 2011
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 9:15pm On Jul 06, 2011
Yeah the answer is 142913828922

Here is my code:
class SumOfPrimes
{
public static void main(String[] args)
{
long num = 2000000;
long sum=0;

for(long i=2; i<=num; i++){
if(isPrime(i)){
sum+=i;
}
}
System.out.println(sum);
}

static boolean isPrime(long num)
{
for(long i=2; i<num; i++)
{
if((num%i)==0){
return false;
}
}
return true;
}
}


@Dell and omo_to_dun, how long did it take your code to compute this?
Re: Coding Challenge 3: Sum Of Primes by Nobody: 9:49pm On Jul 06, 2011

Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 10:19pm On Jul 06, 2011
hahaha you dont want to kno mine, How did you compute that time? UNIX feature or C
Re: Coding Challenge 3: Sum Of Primes by Nobody: 10:24pm On Jul 06, 2011
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 11:36pm On Jul 06, 2011
Yeah it wud and yope the brute force,  I use a Mac, wats d syntax? or rather how o i go about it, I normally use my text editor(TextMate).
Re: Coding Challenge 3: Sum Of Primes by Nobody: 12:05am On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 12:50am On Jul 07, 2011
hahaha yeah i type faster than my static keyboard now adays(effects of recursion), Was asking for the syntax for getting the run time of the program
Re: Coding Challenge 3: Sum Of Primes by tundebabzy: 12:54am On Jul 07, 2011
hahaha. This is a project euler question
Re: Coding Challenge 3: Sum Of Primes by Nobody: 1:21am On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 1:24am On Jul 07, 2011
tundebabzy:

hahaha. This is a project euler question

So i heardsad I have a document filled with some terrible questions, these are the easy ones on the document, lmao
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 1:28am On Jul 07, 2011


Is that it?
Re: Coding Challenge 3: Sum Of Primes by Nobody: 3:44am On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Fayimora(m): 3:48am On Jul 07, 2011
Yaay! thanks that worked!
So which is real, user and sys. What does each calculate
Re: Coding Challenge 3: Sum Of Primes by Nobody: 4:02am On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by dellnet: 5:10am On Jul 07, 2011
@Fayimora, you dont really want to know how long it takes grin grin

@omo, My systems both Linux and Windows output something different from your script.

Re: Coding Challenge 3: Sum Of Primes by Nobody: 6:00am On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Mobinga: 4:02pm On Jul 07, 2011
omo_to_dun:

^
You are absolutely right. It implies that you are using a 32-bit system. My system is 64-bit. I am sorry for not initially making it portable for 32-bit systems. I have updated the code. It should now output correctly on your system. It was [i]s[/i]tupid of me to think that everyone is now running 64-bit systems. In case you are wondering: the answer (142913828922) is greater than the maximum representable positive integer by a size_t data type on a 32-bit system. A size_t happens to be 4 bytes on a 32-bit system, but 8 bytes on a 64-bit system. I have changed it to unsigned long long. Thank you very much. By the way, I noticed that you were running your system as the root user. Chei, oh boy, you get liver o. I don't trust myself to do that.


Wuss.
Re: Coding Challenge 3: Sum Of Primes by Nobody: 4:17pm On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Mobinga: 8:23pm On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Nobody: 8:56pm On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Mobinga: 9:17pm On Jul 07, 2011
omo_to_dun:

LOL. Now implement that in a programming language of your choice and let us analyze the runtime. I assume that you would decline!
I'm doing it, but its maxing out one out of four cores! Kinda like an infinite loop. grin Anyway, I'm still trying, don't want to steal anything off the internet.
Re: Coding Challenge 3: Sum Of Primes by Nobody: 9:42pm On Jul 07, 2011
Re: Coding Challenge 3: Sum Of Primes by Mobinga: 9:55pm On Jul 07, 2011
omo_to_dun:

You have a quad core. That's cool. I should be getting one at the end of the year. Do you honestly notice any overall difference in performance? Or is it when you just use a special type of application?

Honestly, its both. It improves everything, from gta iv to boot time.

@fayimora your code is still compiling, 3 minutes gone.
Re: Coding Challenge 3: Sum Of Primes by Mobinga: 10:33pm On Jul 07, 2011
Phucking Hell.

(1) (2) (Reply)

University/Polytechnic Web Portal: What Features Would You Love? / What Is The URL For Jumia Seller Centre API / Test Your Knowledge Of Excel On This

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.