// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; class MainClass { private const int N = 200000; public static bool isAB1(int n) { for(int a = 2; a <= (int)Math.Sqrt(n); a++) { for(int b = 2; b <= (int)(Math.Log(n)/Math.Log(a) + 0.01); b++) { if(Math.Pow(a, b) == n) { return true; } } } return false; } public static bool isAB2(int n) { int a = 2; while(a*a <= n) { int apowb = a * a; while(apowb <= n) { if(apowb == n) { return true; } apowb *= a; } a++; } return false; } public static void Main(string[] args) { for(int i = 2; i <= 1000; i++) { if(isAB1(i) != isAB2(i)) { Console.WriteLine(i + " " + isAB1(i) + " " + isAB2(i)); } } long t1 = DateTime.Now.Ticks; for(int i = 2; i < N; i++) { isAB1(i); } long t2 = DateTime.Now.Ticks; long t3 = DateTime.Now.Ticks; for(int i = 2; i < N; i++) { isAB2(i); } long t4 = DateTime.Now.Ticks; Console.WriteLine("1: " + (t2 - t1)/1000000 + " 2: " + (t4 - t3)/1000000); } }