Another visualization of Euler 58. This time, using Processing with Java.
Previous .NET winforms visualization of Euler 58
https://projecteuler.net/problem=58
public int i_x = 500; public int i_y = 500; public static int Current_Direction = 0; public int cur_step = 1; public int skip_at = 7; public int skip = 3; public boolean first_turn = false; public int count = 5; public int diag_count = 4; public int prime_count = 2; void setup() { size(1000, 1000); background(153); } void draw(){ cur_step += 1; if ( isPrime(cur_step)) { if (cur_step == skip_at) { fill(44,162,95); } else { fill(153,216,201); } } else { fill(229,245,249); } if (Current_Direction == 0) { rect( i_x + 10, i_y, 10, 10); i_x += 10; } else if (Current_Direction == 3) { rect( i_x, i_y + 10, 10, 10); i_y += 10; } else if (Current_Direction == 2) { rect( i_x - 10, i_y, 10, 10); i_x -= 10; } else if (Current_Direction == 1) { rect( i_x, i_y - 10, 10, 10); i_y -= 10; } if (cur_step <= 3) Turn(); if (cur_step == 5)//2 Turn(); if (skip_at == cur_step) { skip_at += skip; if (first_turn == false) first_turn = true; else { first_turn = false; skip += 1; } Turn(); } } protected void Turn() { if (Current_Direction == 3) Current_Direction = 0; else Current_Direction += 1; } public boolean isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; } |
A fun little project whilst attempting to learn https://processing.org/
I draw 10 x 10 pixel boxes, 100 per row. If the number is a Prime, I highlight it in orange.
Java
//Number Counter public int i_num = 0; //Draw Coordinates public int i_x = 0; public int i_y = 0; void setup() { size(1000, 1000); background(153); } void draw(){ if ( isPrime(i_num)) { fill(204, 102, 0); rect(i_x*10,i_y + 0,10,10); } else { fill(255, 255, 255); rect(i_x*10,i_y + 0,10,10); } i_x +=1; i_num +=1; //Go to Next Line if ( i_x == 100 ) { i_x = 0; i_y += 10; } } public boolean isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; } |