Skip to main content
edited tags
Link
user45941
user45941
Tweeted twitter.com/#!/StackCodeGolf/status/183427840425992192
Put the hint in a spoiler div to make it just a bit harder to see.
Source Link
KeithS
  • 201
  • 1
  • 4

With a twist; your algorithm has to be less complex than O(X2) where X is the Nth prime. Your solution post should include the character count and the theoretical complexity in terms of X (or N, which ~= X/ln(X) and so will usually be more efficient)

Here's a hint solution in C# (XlnXO(Xln(sqrt(X))), 137 chars):

public List<int> P(int n)
{      
   var r = new List<int>{2}; int i=3;
   while(r.Count<n)
      if(!r.TakeWhile(x=>x<Math.Sqrt(i)).Any(x=>i%x==0)) r.Add(i++); else i++;
}

public List<int> P(int n){var r = new List<int>{2}; int i=3; while(r.Count<n) if(!r.TakeWhile(x=>x<Math.Sqrt(i)).Any(x=>i%x==0)) r.Add(i++); else i++; }

With a twist; your algorithm has to be less complex than O(X2) where X is the Nth prime.

Here's a hint solution in C# (XlnX, 137 chars):

public List<int> P(int n)
{      
   var r = new List<int>{2}; int i=3;
   while(r.Count<n)
      if(!r.TakeWhile(x=>x<Math.Sqrt(i)).Any(x=>i%x==0)) r.Add(i++); else i++;
}

With a twist; your algorithm has to be less complex than O(X2) where X is the Nth prime. Your solution post should include the character count and the theoretical complexity in terms of X (or N, which ~= X/ln(X) and so will usually be more efficient)

Here's a hint solution in C# (O(Xln(sqrt(X))), 137 chars):

public List<int> P(int n){var r = new List<int>{2}; int i=3; while(r.Count<n) if(!r.TakeWhile(x=>x<Math.Sqrt(i)).Any(x=>i%x==0)) r.Add(i++); else i++; }

Source Link
KeithS
  • 201
  • 1
  • 4

Return the first N primes

With a twist; your algorithm has to be less complex than O(X2) where X is the Nth prime.

Here's a hint solution in C# (XlnX, 137 chars):

public List<int> P(int n)
{      
   var r = new List<int>{2}; int i=3;
   while(r.Count<n)
      if(!r.TakeWhile(x=>x<Math.Sqrt(i)).Any(x=>i%x==0)) r.Add(i++); else i++;
}