package margusmartseppcode.From_40_to_49;
import java.util.Arrays;
public class Problem_49 {
// Call only if: (n>0&&(i%2==0||i%3==0||i%5==0||i%7==0))
static boolean isPrimeS(final int n) {
final int r = (int) Math.floor(Math.sqrt(n));
for (int f = 5; f <= r; f += 6)
if (n % f == 0 || n % (f + 2) == 0)
return false;
return true;
}
static boolean isPerm(char[] a, char[] b) {
if (a.length != b.length)
return false;
Arrays.sort(a);
Arrays.sort(b);
return Arrays.equals(a, b);
}
static boolean arePerms(int... n) {
if (n.length == 0)
return true;
for (int i = 1; i < n.length; i++)
if (!isPerm(("" + n[i - 1]).toCharArray(), ("" + n[i])
.toCharArray()))
return false;
return true;
}
static boolean arePrimes(int... n) {
for (int i : n)
if (!isPrimeS(i))
return false;
return true;
}
public static void main(String[] args) {
int a, b, c;
a = b = c = 0;
for (a = 1489;; a += 2) {
if (a % 3 == 0 || a % 5 == 0 || a % 7 == 0)
continue;
b = a + 3330;
c = a + 6660;
if (arePrimes(a, b, c))
if (arePerms(a, b, c))
break;
}
System.out.println("" + a + b + c);
}
}
Showing posts with label permutation. Show all posts
Showing posts with label permutation. Show all posts
Sunday, September 13, 2009
Euler Problem 49 solution
Time (s): ~0.002
Labels:
Euler Problem 40-49,
permutation,
prime numbers
Wednesday, September 9, 2009
Euler Problem 43 solution
Time (s): ~0.002
package margusmartseppcode.From_40_to_49;
public class Problem_43 {
static String perms(int n, String s) {
int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 };
return perms(n, s, fact);
}
static String perms(int n, String s, int fact[]) {
StringBuilder sb = new StringBuilder();
StringBuilder s2 = new StringBuilder(s);
n--;
for (int i, g, sl = s2.length(); sl > 0; sl--, n = n % (g)) {
i = (int) Math.floor(n / (g = fact[sl] / sl));
sb.append(s2.charAt(i));
s2.deleteCharAt(i);
}
return sb.toString();
}
static long cint(String nr) {
return Long.parseLong(nr);
}
static boolean check(String s) {
return cint(s.substring(1, 4)) % 2 == 0
&& cint(s.substring(2, 5)) % 3 == 0;
}
public static void main(String[] args) {
long sum = 0;
String p = "";
for (int i = 1; i < 25; i++)
sum += (check(p = perms(i, "0134") + "952867") ? cint(p) : 0)
+ (check(p = perms(i, "0146") + "357289") ? cint(p) : 0);
System.out.println(sum);
}
}
Labels:
Euler Problem 40-49,
pandigital,
permutation
Tuesday, September 8, 2009
Euler Problem 24 solution
Time (s): ~0.001
package margusmartseppcode.From_20_to_29;
public class Problem_24 {
static String perms(int n, String s) {
int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 };
return perms(n, s, fact);
}
static String perms(int n, String s, int fact[]) {
StringBuilder sb = new StringBuilder();
StringBuilder s2 = new StringBuilder(s);
n--;
for (int i, g, sl = s2.length(); sl > 0; sl--, n = n % (g)) {
i = (int) Math.floor(n / (g = fact[sl] / sl));
sb.append(s2.charAt(i));
s2.deleteCharAt(i);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(perms(1000000, "0123456789"));
}
}
Subscribe to:
Posts (Atom)