Truncatable primes: Difference between revisions

Line 370:
 
=={{header|C sharp|C#}}==
<lang csharp>using System; // 4790@3.6
using System.DiagnosticsCollections.Generic;
class truncartable_primes
 
namespace RosettaCode
{
static void Main()
internal class Program
{
public static bool IsPrime(int n)
{
if (n<2) returnuint falsem = 1000000;
Console.Write("L " + L(m) + " R " + R(m) + " ");
if (n<4) return true;
var sbsw = new System.Diagnostics.Stopwatch.StartNew();
if (n%2==0) return false;
if for (n<9int i = 1000; i > 0; i--) return{ trueL(m); R(m); }
if Console.Write(n%3==0sw.Elapsed); return falseConsole.Read();
var r = (int) Math.Sqrt(n);
var f = 6-1;
while (f<=r)
{
if (n%f==0 ||n%(f+2)==0)
return false;
f += 6;
}
return true;
}
 
private static booluint IsRightTruncatableL(intuint n)
{
for ( n -= n & 1; n--;)
for (uint d, d0 = 10, d1 = 100; ; n -= 2)
{
n /= 10;{
if while (n % 3 == 0 || n % 5 == 0 || n % 7 == 0) n -= 2;
return true; if ((d = (n % 10)) == 3 || d == 7)
if (!IsPrime(n)) {
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;
var f = 6-1; }
{ }
}
 
private static booluint IsLeftTruncatableR(intuint nm)
{
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"))
return false;{
for (int i if ((np = n + 1;) i<c.Length>= m) break; i++if (isP(np)) p.Add(np);
if (!IsPrime(Convert.ToInt32np = n + 3) >= m) break; if (c.SubstringisP(i)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;
f += 6;}
return truep[p.Count - 1];
}
 
private static voidbool MainisP(uint n)
{
if (n < 7) return n == 2 || n == 3 || n == 5;
var sb = new Stopwatch();
if ((n & 1) == 0 || n %2 3 == 0 || n % 5 == 0) return false;
sb.Start();
int lt for (uint r = 0(uint)Math.Sqrt(n), rtd = 07; d <= r; d += 30)
for if (intn i% =(d 1000000;+ 00) == i>0; --i|| n % (d + 04) == 0 ||
if ( n %f (d + 06) == 0 || n % (fd +2 10) == 0) ||
{
if n % (IsPrimed + 12) == 0 || n % (i)d + 16) == 0 ||
n % (d + 22) == 0 || n % (d + 24) == 0) return false;
{
if (n<4) return true;
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>
<pre>LargestOutput: truncable leftL is=998443 &R right=739399, calculated in 1624 msec.μs</pre>
 
=={{header|Clojure}}==
Anonymous user