Advertisement
Guest User

Prime Finder

a guest
Nov 2nd, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <form name="primeForm" id="primeForm" method="post">
  2.     <label for="primeLimit">Prime Limit:</label> <input type="text" name="primeLimit" id="primeLimit" /><br />
  3.     <input type="submit" name="submit" id="submit" value="Find Primes!" />
  4. </form>
  5.  
  6. <cfoutput>
  7. <cfif StructKeyExists(FORM, "fieldnames")>
  8.     <cfif IsNumeric(FORM.primeLimit) AND FORM.primeLimit gt 2>
  9.         <cfset vLimit = FORM.primeLimit />
  10.         <cfset primeArr = [] />
  11.         <cfset notPrimeArr = [] />
  12.  
  13.         <!--- start counting from 2 --->
  14.         <cfloop from="2" to="#vLimit#" step="1" index="prime">
  15.             <cfif !ArrayContains(primeArr, prime) AND !ArrayContains(notPrimeArr, prime)>
  16.                 <cfset ArrayAppend(primeArr, prime) />
  17.                 <cfset vSquare = prime * prime />
  18.                 <cfif vSquare lte vLimit>
  19.                     <cfif !ArrayContains(notPrimeArr, vSquare)>
  20.                         <!--- the square is never prime --->
  21.                         <cfset ArrayAppend(notPrimeArr, vSquare) />
  22.                     </cfif>
  23.                     <cfloop from="#vSquare#" to="#vLimit#" step="1" index="primeCounter">
  24.                         <cfif primeCounter lte vLimit>
  25.                             <cfif !ArrayContains(notPrimeArr, primeCounter)>
  26.                                 <cfif primeCounter MOD prime eq 0>
  27.                                     <cfset ArrayAppend(notPrimeArr, primeCounter) />
  28.                                 </cfif>
  29.                             </cfif>
  30.                         <cfelse>
  31.                             <cfbreak />
  32.                         </cfif>
  33.                     </cfloop>
  34.                 <cfelse>
  35.                     <!--- limit is reached, we're done --->
  36.                     <cfbreak />
  37.                 </cfif>
  38.             </cfif>
  39.         </cfloop>
  40.  
  41.         <cfloop from="2" to="#vLimit#" index="finalPrime">
  42.             <cfif !ArrayContains(primeArr, finalPrime) AND !ArrayContains(notPrimeArr, finalPrime)>
  43.                 <cfset ArrayAppend(primeArr, finalPrime) />
  44.             </cfif>
  45.         </cfloop>
  46.         <cfset vTotal = ArrayLen(primeArr) />
  47.  
  48.         <table border="1">
  49.             <cfif vTotal lt 10>
  50.                 <tr>
  51.                     <cfloop array="#primeArr#" index="myPrime">
  52.                         <td>#myPrime#</td>
  53.                     </cfloop>
  54.                 </tr>
  55.             <cfelse>
  56.                 <cfset vCount = 1 />
  57.                 <cfloop array="#primeArr#" index="myPrime">
  58.                     <cfif vCount eq 1 OR vCount MOD 10 eq 1>
  59.                         <tr>
  60.                     </cfif>
  61.                     <td>#myPrime#</td>
  62.                     <cfif vCount mod 10 eq 0>
  63.                         </tr>
  64.                     <cfelseif vCount eq vTotal>
  65.                         <cfloop from="1" to="#10 - (vCount mod 10)#" index="filler">
  66.                             <td>&nbsp;</td>
  67.                         </cfloop>
  68.                         </tr>
  69.                     </cfif>
  70.                     <cfset vCount++ />
  71.                 </cfloop>
  72.             </cfif>
  73.         </table>
  74.     <cfelse>
  75.         Prime Limit must be a number and greater than 2.
  76.     </cfif>
  77. </cfif>
  78. </cfoutput>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement