Code: Selecteer alles
def digit(getal,i):
if i==1:
return getal%10
else:
return digit(getal/10,i-1)
@CloudedHeaven: lijkt mij correct, maar wilt ni compileren bij mij
Code: Selecteer alles
def digit(getal,i):
if i==1:
return getal%10
else:
return digit(getal/10,i-1)
Code: Selecteer alles
digit getal i | i==1 = getal 'mod' 10
| i>1 = digit(getal 'div' 10, i-1)
Ik moest er ff over denken, maar eleganter zal het wel niet opgelost kunnen wordenjhnbk schreef:Code: Selecteer alles
def digit(getal,i): if i==1: return getal%10 else: return digit(getal/10,i-1)
Code: Selecteer alles
public class Test {
public static void main(String[] args) {
Test anObject = new Test();
System.out.println("Start");
anObject.printNumbers();
System.out.println("End");
}
private void printNumbers() {
for (int number = 10000; number < 100000; number++) {
int[] digits = splitNumberIntoDigits(number);
if (noZeros(digits) &&
noDuplicates(digits) &&
firstDigitCondition(digits) &&
secondDigitCondition(digits) &&
thirdDigitCondition(digits) &&
fourthDigitCondition(digits) &&
fifthDigitCondition(digits)) {
System.out.println("A hit: " + number);
}
}
}
private int[] splitNumberIntoDigits(int number) {
int[] digits = new int[5];
String aNumberString = new Integer(number).toString();
for (int i = 0; i < 5; i++) {
digits[i] = Integer.parseInt("" + aNumberString.charAt(i));
}
return digits;
}
private boolean noZeros(int[] digits) {
for (int i = 0; i < 5; i++) {
if (digits[i] == 0) {
return false;
}
}
return true;
}
private boolean noDuplicates(int[] digits) {
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (digits[i] == digits[j]) {
return false;
}
}
}
return true;
}
private boolean firstDigitCondition(int[] digits) {
if (isPrime(digits[0])) {
return true;
}
return false;
}
private boolean isPrime(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
private boolean secondDigitCondition(int[] digits) {
return (digits[1] == digits[4] - digits[0]);
}
private boolean thirdDigitCondition(int[] digits) {
return (digits[2] == 2 * digits[0]);
}
private boolean fourthDigitCondition(int[] digits) {
return (digits[3] == digits[2] + 3);
}
private boolean fifthDigitCondition(int[] digits) {
return (digits[4] == Math.abs(digits[0] - digits[3]));
}
}
ken ik niet, kun je ene link geven met een voorbeeld?Eleganter dan een tail recursion? Ik kan me het niet voorstellen.
Code: Selecteer alles
for a in [2,3,5,7]:
for b in range(1,10):
for c in range(1,10):
for d in range(1,10):
for e in range(1,10):
#a is juist
if b==e-a and c==2*a and d==c+3 and e==d-a:
solution=[a,b,c,d,e]
solution.sort()
double=False
for i in range(5):
try:
if solution[i]==solution[i-1]:
double=True
break
except IndexError:
pass
if double==False:
print a,b,c,d,e
Ik denk dat het de bedoeling is om alle getallen tussen 9999 en 100000 te evalueren (niet alleen de mogelijke getallen). Priemgetallen 'voorzeggen' vind ik ook valsspelen...mijn oplossing voor het probleem in python
Code: Selecteer alles
cout << "23475";
thxMooie oplossing in python.
Code: Selecteer alles
def digit(getal,i):
if i==1:
return getal%10
else:
return digit(getal/10,i-1)
for getal in range(10000,100000):
e=digit(getal,1)
d=digit(getal,2)
c=digit(getal,3)
b=digit(getal,4)
a=digit(getal,5)
if b==e-a and c==2*a and d==c+3 and e==d-a and a in [2,3,5,7]:
solution=[a,b,c,d,e]
solution.sort()
double=False
for i in range(5):
try:
if solution[i]==solution[i-1]:
double=True
break
except IndexError:
pass
if double==False:
print a,b,c,d,e
Code: Selecteer alles
def ispriem(getal):
if getal==1:
return False
for i in range(2,getal):
if getal%i==0:
return False
return True
lui en moe@qrnlk: Google?