77 lines
5.2 KiB
HTML
77 lines
5.2 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>PrimeNumbers.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
||
|
.string {color: #6a8759}
|
||
|
.number {color: #6897bb}
|
||
|
.comment {color: #808080}
|
||
|
.whitespace {color: #505050}
|
||
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
.ST0 {color: #287bde}
|
||
|
.literal {color: #cc7832}
|
||
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/lab14_CalebFontenot/src/lab14_calebfontenot/PrimeNumbers.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> lab14_calebfontenot;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> PrimeNumbers {
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
|
||
|
{
|
||
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">The first 50 prime numbers are: </span><span class="literal">\n</span><span class="string">"</span>);
|
||
|
<span class="ST3">printPrimeNumbers</span>(<span class="number">5</span><span class="number">0</span>);
|
||
|
}
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printPrimeNumbers</span>(<span class="literal">int</span> numberOfPrimes) {
|
||
|
<span class="literal">final</span> <span class="literal">int</span> NUMBER_OF_PRIMES_PER_LINE = <span class="number">10</span>; <span class="comment">// Display 10 per line</span>
|
||
|
<span class="literal">int</span> count = <span class="number">0</span>; <span class="comment">// Count the number of prime numbers</span>
|
||
|
<span class="literal">int</span> number = <span class="number">1</span>; <span class="comment">// A number to be tested for primeness</span>
|
||
|
|
||
|
<span class="comment">//> Repeatedly find prime numbers</span>
|
||
|
<span class="literal">while</span> (count < numberOfPrimes) {
|
||
|
<span class="comment">//>> Print the prime number and increase the count</span>
|
||
|
<span class="literal">if</span> (<span class="ST3">isPrime</span>(number)) {
|
||
|
count++; <span class="comment">// Increase the count</span>
|
||
|
<span class="literal">if</span> (count % NUMBER_OF_PRIMES_PER_LINE == <span class="number">0</span>) {
|
||
|
<span class="comment">//>> Print the number and advance to the new line</span>
|
||
|
System.<span class="ST2">out</span>.printf(<span class="string">"</span><span class="string">%-5d</span><span class="literal">\n</span><span class="string">"</span>, number);
|
||
|
}
|
||
|
<span class="literal">else</span> {
|
||
|
System.<span class="ST2">out</span>.printf(<span class="string">"</span><span class="string">%-5d</span><span class="string">"</span>, number);
|
||
|
}
|
||
|
}
|
||
|
<span class="comment">//>> Check if the next number is prime</span>
|
||
|
number++;
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">isPrime</span>(<span class="literal">int</span> number) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> divisor = <span class="number">2</span>; divisor <= number / <span class="number">2</span>; divisor++) {
|
||
|
<span class="literal">if</span> (number % divisor == <span class="number">0</span>) {
|
||
|
<span class="comment">// If true, number is not prime</span>
|
||
|
<span class="literal">return</span> <span class="literal">false</span>;
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">return</span> <span class="literal">true</span>; <span class="comment">// number is prime</span>
|
||
|
}
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|