Project Euler: Problem 12

By | 31. August 2013

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

public class Problem12 {
 
	static long result = 0;
	 
	public static void solve() {
		long currentnumber = 0;
		long factors = 0;
		long sqrt = 0;
		for (long i = 0; i < Long.MAX_VALUE; i++) {
			currentnumber +=i;
			sqrt = (long)(Math.sqrt(currentnumber));
			for (int j = 1; j < sqrt; j++) {
				if (currentnumber % j == 0) {
					factors+=2;
				}
			}
			if (sqrt*sqrt == currentnumber) {
				factors--;
			}			 
			System.out.println(currentnumber + " has " + factors + " factors");
			if (factors >500) {
				result = currentnumber;
				return;
			}
			factors = 0;
		}
	}
	 
	public static void main(String[] args) {	 
		solve();
		System.out.println(result);
	} 
}

Lösung: 76576500

LCD-Anzeige

By | 19. Juli 2013

Da ich momentan noch an einem größeren Projekt arbeite und dieses eine LCD-Anzeige benötigt, habe ich mir gedacht, ich veröffentliche den Ausschnitt mal.

lcd[1]

Vielleicht ist ja jemand auf der Suche danach oder will sich nicht extra die Mühe machen, selbst etwas zu basteln. Bitte hinterlasst einen Kommentar, falls ihr das Display in euer Programm mit einbauen wollt. Ich bin gespannt, zu was es eingesetzt wird.

Zuerst der Code und dann die Erklärungen:

Start.java

import javax.swing.JFrame;
 
@SuppressWarnings("serial")
public class Start extends JFrame {
 
	Display anzeige;
	 
	public Start() {
		setSize(320, 110);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		anzeige = new Display();
		 
		//Anfangstext
		String text = "jahnke.im";
		for (int i = 0; i < text.length(); i++) {
			anzeige.setCursorPos(i);
			anzeige.setChar(text.charAt(i));
		}
		
		add(anzeige);
		setTitle("LCD-Display");
		 
		setVisible(true);
	}
	 
	public static void main(String[] args) {	 
		new Start();	 
	} 
}

Display.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.JPanel;
 
@SuppressWarnings("serial")
public class Display extends JPanel {
 
	Pixel[][][] display = new Pixel[32][8][5];
	private int position_x = 10;
	private int position_y = 10;
	private int cursorPos = 0;
	 
	private boolean[][][] charTable = new boolean[256][8][5];
	 
	public Display() {
		init();
		for (int i = 0; i < display.length; i++) {
			for (int j = 0; j < display[0].length; j++) {
				for (int k = 0; k < display[0][0].length; k++) {				 
					display[i][j][k] = new Pixel(false, position_x, position_y);
					position_x += 3;				 
				}
				position_x -= 15;
				position_y += 3;
			}
			position_x += 18;
			position_y -= 24;
			if (i == 15) {
				position_x -= 288;
				position_y += 27;
			}
		}
	}
	 
	public void paintComponent(Graphics g) {
		g.setColor(new Color(120, 166, 72));
		g.fillRect(0, 0, 400, 400);
		 
		for (int i = 0; i < display.length; i++) {
			for (int j = 0; j < display[0].length; j++) {
				for (int k = 0; k < display[0][0].length; k++) {				
					if (display[i][j][k].isActivated()) {
						g.setColor(new Color(29, 70, 51));
					} else {
						g.setColor(new Color(91, 159, 73));
					}
					g.fillRect(display[i][j][k].getX(), display[i][j][k].getY(), 2, 2);
				}
			}
		}
		this.repaint();	 
	}
	 
	public void init() {	 
		BufferedImage image;
		File file = new File(this.getClass().getClassLoader().getResource("pixeldata.bmp").getPath());
		try {
			image = ImageIO.read(file);
			for (int i = 0; i < 256; i++) {
				for (int y = 0; y < 8; y++) {
					for (int x = 0; x < 5; x++) {
						if (image.getRGB(x + 5 * i, y) < -1) {
							charTable[i][y][x] = true;
						} else {
							charTable[i][y][x] = false;
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	 
	}
	 
	public void setCursorPos(int pos) {
		cursorPos = pos;
	}
	 
	public void setChar(int chr) {
		int c = chr;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 5; j++) {
				display[cursorPos][i][j].setActivated(charTable[i][j]);
			}
		}
	}
	 
	public void setChar(char chr) {
		int c = (int) chr;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 5; j++) {
				display[cursorPos][i][j].setActivated(charTable[i][j]);
			}
		}
	}
 
}

Pixel.java

public class Pixel {
 
	private boolean activated;
	private int x;
	private int y;
	 
	public Pixel(boolean activated, int x, int y) {
		this.activated = activated;
		this.x = x;
		this.y = y;
	}
	 
	public boolean isActivated() {
		return activated;
	}
	 
	public void setActivated(boolean activated) {
		this.activated = activated;
	}
	 
	public int getX() {
		return x;
	}
	 
	public void setX(int x) {
		this.x = x;
	}
	 
	public int getY() {
		return y;
	}
	 
	public void setY(int y) {
		this.y = y;
	}
}

Es wurde der Zeichensatz des Displays verwendet: (Leichte Abweichung zum originalen ASCII-Code)

zeichensatz[1]

Die verschiedenen Zeichen werden mithilfe der angehängten Bitmap, die beim Programmstart ausgelesen wird, erzeugt.

Link zur Bitmap

Momentan habe ich noch keine Möglichkeit mit eingebaut, den Text zur Laufzeit zu ändern. Aber ich denke, ein Textfeld mit einzubauen sollte für niemanden ein Problem darstellen.

Der Text wird in der Start.java definiert: z.B.:

anzeige.setCursorPos(0);
anzeige.setChar('A');
//oder:
anzeige.setCursorPos(0);
anzeige.setChar(65);

Eine einfache Methode in Display.java genügt, um den Text zur Laufzeit bearbeiten zu können. Diese sollte selbstklärend sein. Andernfalls genügt eine kurze Mail über das Kontaktformular an mich.

Project Euler: Problem 48

By | 20. Mai 2013

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.

BigInteger number = BigInteger.ONE;
BigInteger result = BigInteger.ZERO;
 
for (long i = 1; i &lt;=1000; i++) {
	number = BigInteger.valueOf(i);
	result = result.add(number.pow((int)i));
}
String output = result.toString();
System.out.println(output.substring(output.length()-10));

Lösung: 9110846700

Project Euler: Problem 34

By | 24. April 2013

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

public class Project34 {
 
	public static void main(String[] args) {
		String number = "";
		int tempresult = 0;
		int result = 0;
		for (int i = 3; i < 1000000; i++) {
			number = Integer.toString(i);
			for (int j = 0; j < number.length(); j++) {
				tempresult += fac(Integer.parseInt(String.valueOf(number.charAt(j))));
			}
			System.out.println(i + " " + tempresult);
			if (i==tempresult) {
				System.out.println(i);
				result += i;
			}
			tempresult = 0;
		}
		System.out.println("Ergebnis: " + result);	 
	}
	
	public static int fac(int number){
		if (number==1 || number == 0) {
			return 1;
		}
		number = number*fac(number-1);
		return number;	 
	} 
}

Lösung: 40730

Project Euler: Problem 30

By | 24. April 2013

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

int sum = 0;
String temp = "";
int tempresult = 0;
for (int i = 0; i < 236196; i++) {
	temp = Integer.toString(i);
	for (int j = 0; j < temp.length(); j++) {
		tempresult += Math.pow(Integer.parseInt(String.valueOf(temp.charAt(j))), 5);
	}
	if (tempresult == i && !(i==0||i==1)) {
		System.out.println(i);
		sum += i;
	}
	tempresult = 0;
}
System.out.println("Ergebnis:" + sum);

Lösung: 443839

Project Euler: Problem 25

By | 24. April 2013

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2 , where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12 , is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

BigInteger number1 = BigInteger.ZERO;
BigInteger number2 = BigInteger.ONE;
BigInteger temp = BigInteger.ZERO;
int count = 1;
 
while (number2.toString().length()&lt;1000) {
	temp = number1.add(number2);
	number1 = number2;
	number2 = temp;
	count++;
}
System.out.println(count);

Lösung: 4782

Project Euler: Problem 20

By | 24. April 2013

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

int result = 0;
BigInteger temp = BigInteger.ONE;
 
for (long i=2; i&lt;=100; i++) {
	temp = temp.multiply(BigInteger.valueOf(i));
}
String number = temp.toString();
for (int i=0; i&lt;number.length(); i++) {
	result += Character.digit(number.charAt(i), 10);
}
System.out.println(result);

Lösung: 648

Project Euler: Problem 10

By | 20. April 2013

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

long result = 0;
boolean[] gestrichen = new boolean[limit];
for (int i=2; i&lt;gestrichen.length; i++) {
	gestrichen[i] = false;
}
 
for (int i=2; i&lt;gestrichen.length; i++) {
	if (!gestrichen[i]) {
		System.out.println(i + " ");
		result += (long)i;
		for (int j=2*i; j&lt;limit; j=j+i) {
			gestrichen[j] = true;
		}
	}
}
System.out.println("=============");
System.out.println(result);

Lösung: 142913828922