Truncatable primes: Difference between revisions
Content deleted Content added
Line 370: | Line 370: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<lang csharp>using System; |
<lang csharp>using System; // 4790@3.6 |
||
using System. |
using System.Collections.Generic; |
||
class truncartable_primes |
|||
namespace RosettaCode |
|||
{ |
{ |
||
static void Main() |
|||
internal class Program |
|||
{ |
|||
public static bool IsPrime(int n) |
|||
{ |
{ |
||
uint m = 1000000; |
|||
Console.Write("L " + L(m) + " R " + R(m) + " "); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for (int i = 1000; i > 0; i--) { L(m); R(m); } |
|||
Console.Write(sw.Elapsed); Console.Read(); |
|||
var r = (int) Math.Sqrt(n); |
|||
⚫ | |||
while (f<=r) |
|||
⚫ | |||
⚫ | |||
return false; |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
static uint L(uint n) |
|||
{ |
{ |
||
n -= n & 1; n--; |
|||
for (uint d, d0 = 10, d1 = 100; ; n -= 2) |
|||
{ |
|||
{ |
|||
while (n % 3 == 0 || n % 5 == 0 || n % 7 == 0) n -= 2; |
|||
if ((d = (n % 10)) == 3 || d == 7) |
|||
{ |
|||
while (d1 < n && n % d1 > n % d0 && isP(n % d1)) |
|||
return false; |
|||
{ d1 *= 10; d0 *= 10; } |
|||
} |
|||
if (d1 > n && isP(n)) return n; d0 = 10; d1 = 100; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
static uint R(uint m) |
|||
{ |
{ |
||
var p = new List<uint>() { 2, 3, 5, 7 }; uint n = 20, np; |
|||
string c = n.ToString(); |
|||
for (int i = 1; i < p.Count; n = 10 * p[i++]) |
|||
if (c.Contains("0")) |
|||
{ |
|||
if ((np = n + 1) >= m) break; if (isP(np)) p.Add(np); |
|||
if ( |
if ((np = n + 3) >= m) break; if (isP(np)) p.Add(np); |
||
if ((np = n + 7) >= m) break; if (isP(np)) p.Add(np); |
|||
return false; |
|||
if ((np = n + 9) >= m) break; if (isP(np)) p.Add(np); |
|||
return true; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
static bool isP(uint n) |
|||
{ |
{ |
||
if (n < 7) return n == 2 || n == 3 || n == 5; |
|||
⚫ | |||
⚫ | |||
sb.Start(); |
|||
for (uint r = (uint)Math.Sqrt(n), d = 7; d <= r; d += 30) |
|||
if (n % (d + 00) == 0 || n % (d + 04) == 0 || |
|||
⚫ | |||
{ |
|||
n % (d + 12) == 0 || n % (d + 16) == 0 || |
|||
n % (d + 22) == 0 || n % (d + 24) == 0) return false; |
|||
{ |
|||
⚫ | |||
if (rt==0 && IsRightTruncatable(i)) |
|||
rt = i; |
|||
else if (lt==0 && IsLeftTruncatable(i)) |
|||
lt = i; |
|||
if (lt!=0 && rt!=0) |
|||
break; |
|||
} |
|||
} |
|||
sb.Stop(); |
|||
Console.WriteLine("Largest truncable left is={0} & right={1}, calculated in {2} msec.", |
|||
lt, rt, sb.ElapsedMilliseconds); |
|||
} |
} |
||
} |
|||
}</lang> |
}</lang> |
||
<pre> |
<pre>Output: L 998443 R 739399 24 μs</pre> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |