The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
PE37
Posted by: calebrob6 | May 10, 2011 @ 12:53am
Java Code
[
Download
]
package pe37test; /** * * @author Caleb */ public class Main { static final int clim = 1000000; static boolean nums[]; public static void main(String[] args) { long t1 = System.nanoTime(); int sum=0; findPrimes(); long t2 = System.nanoTime(); System.out.println("Generates primes in: " + ((t2 - t1) * 1e-6) + " milliseconds"); for (int i = 0; i < clim; i++) { if (nums[i]) { if (truncateCheck(i)) { System.out.println(i); sum += i; } } } System.out.println("Sum:"+sum); t2 = System.nanoTime(); System.out.println("Execution time: " + ((t2 - t1) * 1e-6) + " milliseconds"); } public static boolean truncateCheck(int number) { for (int i = 1; i < String.valueOf(number).length(); i++) { if (nums[Integer.parseInt(String.valueOf(number).substring(i))]==false) { return false; } if (nums[Integer.parseInt(String.valueOf(number).substring(0, i))] == false) { return false; } } return true; } public static void findPrimes() { nums = new boolean[clim + 1]; for (int i = 2; i <= clim; i++) { nums[i] = true; } for (int i = 2; i * i <= clim; i++) { if (nums[i]) { for (int j = i; i * j <= clim; j++) { nums[i * j] = false; } } } } }
Syntax Highlighting
[
Open in new window
]
Author Comments
Runs in .07 seconds
Rating
4.56 / 8
198 Votes
http://codebin.yi.org/1261
page generated in 0.01 seconds