Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form name="primeForm" id="primeForm" method="post">
- <label for="primeLimit">Prime Limit:</label> <input type="text" name="primeLimit" id="primeLimit" /><br />
- <input type="submit" name="submit" id="submit" value="Find Primes!" />
- </form>
- <cfoutput>
- <cfif StructKeyExists(FORM, "fieldnames")>
- <cfif IsNumeric(FORM.primeLimit) AND FORM.primeLimit gt 2>
- <cfset vLimit = FORM.primeLimit />
- <cfset primeArr = [] />
- <cfset notPrimeArr = [] />
- <!--- start counting from 2 --->
- <cfloop from="2" to="#vLimit#" step="1" index="prime">
- <cfif !ArrayContains(primeArr, prime) AND !ArrayContains(notPrimeArr, prime)>
- <cfset ArrayAppend(primeArr, prime) />
- <cfset vSquare = prime * prime />
- <cfif vSquare lte vLimit>
- <cfif !ArrayContains(notPrimeArr, vSquare)>
- <!--- the square is never prime --->
- <cfset ArrayAppend(notPrimeArr, vSquare) />
- </cfif>
- <cfloop from="#vSquare#" to="#vLimit#" step="1" index="primeCounter">
- <cfif primeCounter lte vLimit>
- <cfif !ArrayContains(notPrimeArr, primeCounter)>
- <cfif primeCounter MOD prime eq 0>
- <cfset ArrayAppend(notPrimeArr, primeCounter) />
- </cfif>
- </cfif>
- <cfelse>
- <cfbreak />
- </cfif>
- </cfloop>
- <cfelse>
- <!--- limit is reached, we're done --->
- <cfbreak />
- </cfif>
- </cfif>
- </cfloop>
- <cfloop from="2" to="#vLimit#" index="finalPrime">
- <cfif !ArrayContains(primeArr, finalPrime) AND !ArrayContains(notPrimeArr, finalPrime)>
- <cfset ArrayAppend(primeArr, finalPrime) />
- </cfif>
- </cfloop>
- <cfset vTotal = ArrayLen(primeArr) />
- <table border="1">
- <cfif vTotal lt 10>
- <tr>
- <cfloop array="#primeArr#" index="myPrime">
- <td>#myPrime#</td>
- </cfloop>
- </tr>
- <cfelse>
- <cfset vCount = 1 />
- <cfloop array="#primeArr#" index="myPrime">
- <cfif vCount eq 1 OR vCount MOD 10 eq 1>
- <tr>
- </cfif>
- <td>#myPrime#</td>
- <cfif vCount mod 10 eq 0>
- </tr>
- <cfelseif vCount eq vTotal>
- <cfloop from="1" to="#10 - (vCount mod 10)#" index="filler">
- <td> </td>
- </cfloop>
- </tr>
- </cfif>
- <cfset vCount++ />
- </cfloop>
- </cfif>
- </table>
- <cfelse>
- Prime Limit must be a number and greater than 2.
- </cfif>
- </cfif>
- </cfoutput>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement