Programming Challenge

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

For all the programmers out there, I'm throwing out a challenge. Who ever got the best result will get Sajha Programmer Award. lol Here's the challenge. Using any programming language, find out the largest nth prime in under 60 seconds. Which means: you have to write a most efficient code to compute the largest prime within the limit of 1 minute. (For example: give input of 100000. code has to compute 1,00,000th prime number which is 1,299,709. But it has to finish the execution under 60 second. )

helpjava11 · Aug 19, 2015 12:50 PM · 20,147 views

12 Replies

What kind of question is this? You understand compute time depends on machine's CPU, RAM, programs it's running etc. Even "Hello World" could take days.

sajhamitra · Aug 19, 2015 12:55 PM

It would be very difficult to find a machine that takes days to run Hello world now. lol But you are right, sajhamitra, use the best PC you can get access to and mention the specs when you post the result.

helpjava11 · Aug 19, 2015 2:21 PM

जाभा ब्रो, मैले भी.बि डट नेटमा कोड गरेको हो है ! मेरो रिजल्ट सेकेन्डमा नभई मिलिसेकेंदमा छ ! तेसैले यो पुरस्कार मलाई नै जान्छ भन्ने आशा लिएको छु है त् ब्रो ! Module Module1     Sub Main()         Dim ps As Class1 = New Class1()         Dim result As New List(Of Integer)         Dim sw As Stopwatch = New Stopwatch()         sw.Start()         result = ps.SieveOfEratosthenes(1299719)         sw.Stop()         Dim counter As Double         For Each item In result             Console.WriteLine(item)             counter = counter + 1         Next         Console.WriteLine("Total Time Elapsed: {0}", sw.Elapsed)         Console.WriteLine("Nth position: {0}", counter)         Console.WriteLine("Programmed by Nasty Nas :)")         Console.ReadLine()     End Sub End Module Public Class Class1     Public Function SieveOfEratosthenes(ByVal N As Integer) As List(Of Integer)         'Stores all the prime numbers         Dim result As New List(Of Integer)         'We need an boolean array that indicates if the number is a prime         Dim IsPrime As New List(Of Boolean)         'Fill the array with all true values and we will start at the number 2         For i As Integer = 0 To N - 2             IsPrime.Add(True)         Next         'Find and store how many numbers we need to check         Dim NumberOfPrimeChecks As Integer = CInt(Math.Sqrt(N))         'Start checking at the number 2         Dim CurrentNumber As Integer = 2         'Loop through all the nessecary checks          For i As Integer = 0 To NumberOfPrimeChecks - 1             If IsPrime(i) Then                 'if the number 2 is prime the number 4 is not etc...                 For j As Integer = i + CurrentNumber To IsPrime.Count - 1 Step CurrentNumber                     IsPrime(j) = False                 Next             End If             'Increments the counter             CurrentNumber += 1         Next         'Print the stored result in our list based on the boolean values         For i As Integer = 0 To IsPrime.Count - 1             If IsPrime(i) Then                 result.Add(i + 2)             End If         Next         Return result     End Function End Class

Nas · Aug 19, 2015 2:48 PM

Last edited: 19-Aug-15 03:15 PM

NepaliBhai · Aug 19, 2015 3:12 PM

Hey Nas, I appreciate your sharpness on googling and cut pasting the code. There are tons of code you can find with the word SieveOfEratosthenes function in various languages. At least you should have changed the function name and some variable names and return variable name. Anyway, I appreciate your aggressiveness in exploring the solutions. By the way, I don't like VB.net. I am C# guy. Last edited: 19-Aug-15 03:14 PM

NepaliBhai · Aug 19, 2015 3:12 PM

400000 th prime is 5800079 56.246999979 seconds Process finished with exit code 0 So far i can find 400,000th prime within 1 minute. Nas bro, try to find the largest prime within 1 minutes limit..

helpjava11 · Aug 19, 2015 3:17 PM

"At least you should have changed the function name and some variable names and return variable name." नेपालीभाई ब्रो ! त्यो कलेजमा साथीको कोड कपि गरेर फंक्सन नेम चेन्ज गरेर प्रोफेसरलाइ बुझाने असैन्मेंट जस्तो कहा हो र यो च्यालेन्ज ! किनकी हाम्रो जाभा ब्रो अल्गोरिदमका गुरु हुनुहुन्छ, मैले सिब अफ एरातोसठेनेस को अल्गोरिथ्म इउज गरेको हुँ भनेर देखाउन, एक्स्प्लिसितली तेही फंक्सन इउज गरेको हुँ ! जाभाब्रोलाइ पक्कै पनि त्येस बारे थाहा हुँन पर्छ किनकि यो मोस्ट एफ़िसिएन्त अल्गोरिथ्म हो, यो प्रब्लम सल्भ गर्नको लागि ! म पनि सी शार्प नै हुँ ! तर अहिलेको यो प्रोजेक्ट चै भी बि हो ! मलाई सी शार्प मन पर्छ तर अहिले यो प्रोजेक्टले गर्दा अलिक भी बिको पनि माया लाग्न थाल्यो ! अनि अहिलेको देभेलोपमेन्ट साइकल भनेकै ....

Nas · Aug 19, 2015 3:40 PM

जाभा ब्रो खोइ त् मेरो "साझा प्रोग्र्यमर अवार्ड" ! हिजो झन् काममा लुकी लुकी आफ्नो काम नगरी यो प्रोग्राम लेखि बसेको ! ल प्लिज एउटा सर्टिफिकेट को जेपीजी भएनी पुरस्कार दिन पर्यो नि ब्रो ! म मेरो प्रोफाइलमा सजाएर राक्चु नि !

Nas · Aug 20, 2015 11:35 AM

helpjava11 · Aug 20, 2015 12:02 PM

आइ एम सो प्राउद अफ माइ एचिभमेन्ट इन साझा ! थ्याँकिउ सोमच जाभा ब्रो 😄

Nas · Aug 20, 2015 12:38 PM

Is this correct? Runs for a minute, output is 1137030nth prime number is 17752279 import java.util.concurrent.TimeUnit; public class PrimeTest { public static void main(final String args[]) { long start = System.currentTimeMillis(); long currentTime = start; long count = 0; long numberToCheck = 1; long primeNumber = numberToCheck; while(start < currentTime + TimeUnit.SECONDS.toMillis(60)) { if (isPrime(numberToCheck)) { count ++; primeNumber = numberToCheck; } numberToCheck ++; start = System.currentTimeMillis(); } System.out.println(count + "th prime number is " + primeNumber); } /** * @param numberToCheck * @return */ private static boolean isPrime(final long numberToCheck) { for (int divisor = 2; divisor < Math.sqrt(numberToCheck); divisor ++) { if (numberToCheck % divisor == 0) { return false; } } return true; } }

prankster · Aug 20, 2015 5:48 PM

Prankster : the result is impressive >1.1m th prime within a minute, but the answer doesn't look correct.

helpjava11 · Aug 20, 2015 6:45 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion