daGrevis Posted January 29, 2014 Report Posted January 29, 2014 Clojure, versija #2. http://vpaste.net/CzQ61 Quote
daGrevis Posted January 30, 2014 Report Posted January 30, 2014 Clojure, versija #3. Izpildās apmēram .9 sekundēs. http://vpaste.net/tTLyI Codez, cik ātri tava Scala izskrien cauri? Quote
codez Posted January 30, 2014 Report Posted January 30, 2014 Šis variants: 0L to 9876543210L by 2013 map(_.toString())filter(a=>(a.distinct.size==a.size && a.contains("2013"))) map println 3.8 sekundes, no kurām 1 sekunde ir to _.toString un 2.4 ir a.distinct Izveidojot ar pieeju kāda ir c++ kodā: def isUniqueDigits(a:Long):Boolean={ var b=a; var arr=Array(0,0,0,0,0,0,0,0,0,0) while (b>0) { if (arr((b%10).toInt)==1) return false arr((b%10).toInt)=1 b=b/10 } true } def contains2013(a:Long):Boolean={ var b=a while (b>0) { if (b%10000==2013) return true b=b/10 } false } 0L to 9876543210L by 2013 foreach(a=>if (isUniqueDigits(a) && contains2013(a)) println(a)) Laiks:0.42 sekundes - jāpiezīmē, ka tas ir labāks kā c++ kodam. Quote
codez Posted January 30, 2014 Report Posted January 30, 2014 Sadalot visu intervālu mazākos intervālos un padodot to izpildīt paralēli: 0.27 sekundes def isUniqueDigits(a:Long):Boolean={ var b=a; var arr=Array(0,0,0,0,0,0,0,0,0,0) while (b>0) { if (arr((b%10).toInt)==1) return false arr((b%10).toInt)=1 b=b/10 } true } def contains2013(a:Long):Boolean={ var b=a while (b>0) { if (b%10000==2013) return true b=b/10 } false } var seqOfWorks=for{ i <- 0L to 9876543210L by 20130000 } yield i to i+20130000 by 2013 seqOfWorks.par.map(_.filter(a=>isUniqueDigits(a) && contains2013(a)).map(println)) Šis viss strādātu vēl ātrāk, ja notiktu Int robežās, jo kā par nelaimi, Scalai ir paralēlu intervālu klase, bet strādā tikai ar int intervāliem. Quote
Bremze Posted January 30, 2014 Author Report Posted January 30, 2014 Paldies par atbildēm! Skatos jūs piedūrāties :D. Prasīju pēc risinājuma, nevis ātruma griestiem, bet tpt prieks! Quote
daGrevis Posted January 30, 2014 Report Posted January 30, 2014 Bāc, būs man arī jāpamēģina paralizēt! Quote
Kavacky Posted January 30, 2014 Report Posted January 30, 2014 Ar paralizēšanu izpildes laiks -> ∞. Quote
ieleja Posted January 31, 2014 Report Posted January 31, 2014 (edited) neoptimizēta Java: package fig; public class Fig { public static int prepFig (String figString) { int fig[] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < figString.length (); i++) if (++fig[figString.charAt(i) - 48] > 1) return 0; return 1; } public static void main(String[] args) { for (long i = 2013; i <= 9876543210L; i = i + 2013) { String figString = Long.toString(i); if (figString.contains("2013")) if (prepFig (figString) == 1) System.out.println(i); } } } 0.62 s gan jau ir vērts norādīt CPU, RAM un OS i5-2520M @ 2,50 GHz, 16 GB, Win7-64 Edited January 31, 2014 by ieleja Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.