Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: HTML  |  size: 30.94 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. curl -v http://blog.jgc.org/2012/04/make-your-own-prime-factorization.html
  2. * About to connect() to blog.jgc.org port 80 (#0)
  3. *   Trying 199.27.135.39... connected
  4. * Connected to blog.jgc.org (199.27.135.39) port 80 (#0)
  5. > GET /2012/04/make-your-own-prime-factorization.html HTTP/1.1
  6. > User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
  7. > Host: blog.jgc.org
  8. > Accept: */*
  9. >
  10. < HTTP/1.1 200 OK
  11. < Server: cloudflare-nginx
  12. < Date: Mon, 30 Apr 2012 13:48:21 GMT
  13. < Content-Type: text/html; charset=UTF-8
  14. < Transfer-Encoding: chunked
  15. < Connection: keep-alive
  16. < Expires: Mon, 30 Apr 2012 13:48:21 GMT
  17. < Cache-Control: private, max-age=0
  18. < Last-Modified: Mon, 30 Apr 2012 13:37:33 GMT
  19. < ETag: "0a212812-76a8-4a14-84f1-2632e919a1a5"
  20. < X-Content-Type-Options: nosniff
  21. < X-XSS-Protection: 1; mode=block
  22. < Set-Cookie: __cfduid=dca600a2f5662181495add3847a3a1c251335793701; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.jgc.org
  23. < Set-Cookie: __cfduid=dca600a2f5662181495add3847a3a1c251335793701; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.blog.jgc.org
  24. <
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  26. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  27. <head>
  28. <meta name="tinfoil-site-verification" content="bc65ff3435c058337604023eb334d70ce7454f4c"/>
  29. <script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>
  30. <title>Make your own 'prime factorization' diagram | John Graham-Cumming</title>
  31. <meta property="fb:admins" content="632495319"/>
  32. <script type="text/javascript">(function() { var a=window;function c(b){this.t={};this.tick=function(b,i,d){d=void 0!=d?d:(new Date).getTime();this.t[b]=[d,i]};this.tick("start",null,b)}var f=new c;a.jstiming={Timer:c,load:f};try{var g=null;a.chrome&&a.chrome.csi&&(g=Math.floor(a.chrome.csi().pageT));null==g&&a.gtbExternal&&(g=a.gtbExternal.pageT());null==g&&a.external&&(g=a.external.pageT);g&&(a.jstiming.pt=g)}catch(h){};a.tickAboveFold=function(b){var e=0;if(b.offsetParent){do e+=b.offsetTop;while(b=b.offsetParent)}b=e;750>=b&&a.jstiming.load.tick("aft")};var j=!1;function k(){j||(j=!0,a.jstiming.load.tick("firstScrollTime"))}a.addEventListener?a.addEventListener("scroll",k,!1):a.attachEvent("onscroll",k);
  33.  })();</script><script type="text/javascript">(function(){var a;var b=navigator.userAgent;a=-1!=b.indexOf("Mobile")&&-1!=b.indexOf("WebKit")&&-1==b.indexOf("iPad")?!0:-1!=b.indexOf("Opera Mini")?!0:!1;if(a){var c;a:{var d=window.location.href,e=d.split("?");switch(e.length){case 1:c=d+"?m=1";break a;case 2:c=0<=e[1].search("(^|&)m=")?null:d+"&m=1";break a;default:c=null}}c&&window.location.replace(c)};})();
  34. </script><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  35. <meta name="MSSmartTagsPreventParsing" content="true"/>
  36. <meta name="generator" content="Blogger"/>
  37. <link rel="icon" type="image/vnd.microsoft.icon" href="http://www.blogger.com/favicon.ico"/>
  38. <link rel="alternate" type="application/atom+xml" title="John Graham-Cumming - Atom" href="http://blog.jgc.org/feeds/posts/default"/>
  39. <link rel="alternate" type="application/rss+xml" title="John Graham-Cumming - RSS" href="http://blog.jgc.org/feeds/posts/default?alt=rss"/>
  40. <link rel="service.post" type="application/atom+xml" title="John Graham-Cumming - Atom" href="http://www.blogger.com/feeds/19303585/posts/default"/>
  41. <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.blogger.com/rsd.g?blogID=19303585"/>
  42. <link rel="alternate" type="application/atom+xml" title="John Graham-Cumming - Atom" href="http://blog.jgc.org/feeds/6665439025357951622/comments/default"/>
  43. <link rel="stylesheet" type="text/css" href="http://www.blogger.com/static/v1/v-css/50269083-blog_controls.css"/> <link rel="stylesheet" type="text/css" href="http://www.blogger.com/dyn-css/authorization.css?targetBlogID=19303585&zx=0a212812-76a8-4a14-84f1-2632e919a1a5"/><script type="text/javascript" src="http://www.blogger.com/static/v1/v-js/979395223-backlink.js"></script>
  44. <script type="text/javascript" src="http://www.blogger.com/static/v1/v-js/1895108979-backlink_control.js"></script>
  45. <script type="text/javascript">var BL_backlinkURL = "http://www.blogger.com/dyn-js/backlink_count.js";var BL_blogId = "19303585";</script>
  46. <link href="http://usethesource.com/prettify.css" type="text/css" rel="stylesheet"/>
  47. <style type="text/css">body{}a img{border-width:0;}#header{width:660px;margin:0 auto 10px;border:1px solid #ccc;}#blog-title{margin:5px 5px 0;padding:20px 20px .25em;border:1px solid #eee;border-width:1px 1px 0;font-size:200%;line-height:1.2em;font-weight:normal;color:#666;text-transform:uppercase;}#blog-title a{color:#666;text-decoration:none;}#blog-title a:hover{color:#c60;}#description{margin:0 5px 5px;padding:0 20px 20px;border:1px solid #eee;border-width:0 1px 1px;max-width:700px;font:Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;color:#999;}#content{margin:none;padding-left:0px;text-align:left;}#main{width:760px;float:left;margin-left:10px;padding-left:10px;padding-right:10px;border:1px solid #FCE8AF}#sidebar{width:220px;float:left;padding:20px;}h2{margin:1.5em 0 .75em;font:Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;color:#999;}.date-header{margin:1.5em 0 .5em;}.post{margin:.5em 0 1.5em;border-bottom:1px dotted #ccc;padding-bottom:1.5em;font-size:12pt;}.post-title{margin:.25em 0 0;padding:0 0 4px;font-weight:normal;line-height:1.4em;color:#c60;}.post-title a,.post-title a:visited,.post-title strong{display:block;text-decoration:none;color:#c60;font-weight:normal;}.post-title strong,.post-title a:hover{color:#333;}.post div{margin:0 0 .75em;line-height:1.6em;}p.post-footer{margin:-.25em 0 0;color:#ccc;}.post-footer em,.comment-link{font:Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;}.post-footer em{font-style:normal;color:#999;margin-right:.6em;}.comment-link{margin-left:.6em;}.post img{padding:4px;}.post blockquote{margin:1em 20px;}.post blockquote p{margin:.75em 0;}#comments h4{margin:1em 0;font:bold Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;color:#999;}#comments h4 strong{font-size:130%;}#comments-block{margin:1em 0 1.5em;line-height:1.6em;}#comments-block dt{margin:.5em 0;}#comments-block dd{margin:.25em 0 0;}#comments-block dd.comment-timestamp{margin:-.25em 0 2em;font:Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;}#comments-block dd p{margin:0 0 .75em;}.deleted-comment{font-style:italic;color:gray;}#sidebar ul{margin:0 0 1.5em;padding:0 0 1.5em;border-bottom:1px dotted #ccc;list-style:none;}#sidebar li{margin:0;padding:0 0 .25em 15px;text-indent:-15px;line-height:1.5em;}#sidebar p{color:#666;line-height:1.5em;}#profile-container{margin:0 0 1.5em;border-bottom:1px dotted #ccc;padding-bottom:1.5em;}.profile-datablock{margin:.5em 0 .5em;}.profile-img{display:inline;}.profile-img img{float:left;padding:4px;border:1px solid #ddd;margin:0 8px 3px 0;}.profile-data{margin:0;font:bold Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;}.profile-data strong{display:none;}.profile-textblock{margin:0 0 .5em;}.profile-link{margin:0;font:Georgia,"Times New Roman",Times,"New York",serif text-transform:uppercase;}#footer{margin:0;clear:both;}#footer hr{display:none;}#footer p{margin:0;padding-top:15px;font:Georgia,"Times New Roman",Times,"New York",serif}</style>
  48. <link title="jgcstyle" rel="stylesheet" href="http://www.jgc.org/styles.css" type="text/css">
  49. <script type="text/javascript" src="http://usethesource.com/prettify.js"></script>
  50. <SCRIPT TYPE="text/javascript">
  51. <!--
  52.  
  53. function HideContent(d) {
  54. if(d.length < 1) { return; }
  55. document.getElementById(d).style.display = "none";
  56. }
  57. function ShowContent(d) {
  58. if(d.length < 1) { return; }
  59. document.getElementById(d).style.display = "block";
  60. }
  61. function ReverseContentDisplay(d) {
  62. if(d.length < 1) { return; }
  63. if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
  64. else { document.getElementById(d).style.display = "none"; }
  65. }
  66.  
  67. function newImage(arg) {
  68.         if (document.images) {
  69.                 rslt = new Image();
  70.                 rslt.src = arg;
  71.                 return rslt;
  72.         }
  73. }
  74.  
  75. function changeImages() {
  76.         if (document.images && (preloadFlag == true)) {
  77.                 for (var i=0; i<changeImages.arguments.length; i+=2) {
  78.                         document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
  79.                 }
  80.         }
  81. }
  82.  
  83. var preloadFlag = false;
  84. function preloadImages() {
  85.         if (document.images) {
  86.                 menu_09_over = newImage("http://www.jgc.org/images/menu_09-over.gif");
  87.                 menu_10_over = newImage("http://www.jgc.org/images/menu_10-over.gif");
  88.                 menu_11_over = newImage("http://www.jgc.org/images/menu_11-over.gif");
  89.                 menu_12_over = newImage("http://www.jgc.org/images/menu_12-over.gif");
  90.                 menu_13_over = newImage("http://www.jgc.org/images/menu_13-over.gif");
  91.                 menu_14_over = newImage("http://www.jgc.org/images/menu_14-over.gif");
  92.                 menu_15_over = newImage("http://www.jgc.org/images/menu_15-over.gif");
  93.                 menu_16_over = newImage("http://www.jgc.org/images/menu_16-over.gif");
  94.                 menu_18_over = newImage("http://www.jgc.org/images/menu_18-over.gif");
  95.                 preloadFlag = true;
  96.         }
  97. }
  98.  
  99. // -->
  100. </SCRIPT>
  101.  <style type="text/css">@import url(http://www.blogger.com/static/v1/v-css/navbar/697174003-classic.css);div.b-mobile{display:none;}</style>
  102. <meta name="pinterest" content="nopin"/></head>
  103. <BODY ONLOAD="preloadImages(); prettyPrint();"><script type="text/javascript">
  104. //<![CDATA[
  105. try{(function(a){var b="http://",c="blog.jgc.org",d="/cdn-cgi/cl/",e="197ea504ddebf89f3a5e6bfcb64899d380b1abe4.gif",f=new a;f.src=[b,c,d,e].join("")})(Image)}catch(e){}
  106. //]]>
  107. </script><script type="text/javascript">
  108.     function setAttributeOnload(object, attribute, val) {
  109.       if(window.addEventListener) {
  110.         window.addEventListener('load',
  111.           function(){ object[attribute] = val; }, false);
  112.       } else {
  113.         window.attachEvent('onload', function(){ object[attribute] = val; });
  114.       }
  115.     }
  116.   </script>
  117. <iframe src="http://www.blogger.com/navbar.g?targetBlogID=19303585&amp;blogName=John+Graham-Cumming&amp;publishMode=PUBLISH_MODE_HOSTED&amp;navbarType=BLUE&amp;layoutType=CLASSIC&amp;searchRoot=http://blog.jgc.org/search&amp;blogLocale=en_US&amp;homepageUrl=http://blog.jgc.org/&amp;vt=4796718745859536663" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" height="30px" width="100%" id="navbar-iframe" allowtransparency="true" title="Blogger Navigation and Search"></iframe>
  118. <div></div>
  119. <div id="content">
  120. <br/>
  121. <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  122. <tr>
  123. <td width="100%" style="background-image: url('http://www.jgc.org/images/index_01.gif'); background-repeat: repeat-x; background-position: left top" height="17">
  124. </td>
  125. </tr>
  126. <tr>
  127. <td width="100%">
  128. <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber2">
  129. <tr>
  130. <td>
  131. <IMG SRC="http://www.jgc.org/images/index_03.gif" WIDTH=12 HEIGHT=56 ALT=""></td>
  132. <td>
  133. <IMG SRC="http://www.jgc.org/images/index_04.gif" WIDTH=253 HEIGHT=56 ALT=""></td>
  134. <td>
  135. <IMG SRC="http://www.jgc.org/images/index_05.gif" WIDTH=459 HEIGHT=56 ALT=""></td>
  136. <td>
  137. <IMG SRC="http://www.jgc.org/images/index_06.gif" WIDTH=263 HEIGHT=56 ALT=""></td>
  138. <td style="background-image: url('http://www.jgc.org/images/index_07.gif'); background-repeat: repeat-x; background-position: left top" width="100%">&nbsp;</td>
  139. <td>
  140. <IMG SRC="http://www.jgc.org/images/index_08.gif" WIDTH=11 HEIGHT=56 ALT=""></td>
  141. </tr>
  142. </table>
  143. </td>
  144. </tr>
  145. <tr>
  146. <td width="100%" style="background-image: url('http://www.jgc.org/images/index_16.gif'); background-repeat: repeat-x; background-position: left top">
  147. <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1">
  148. <tr>
  149. <td>
  150. <A HREF="http://jgc.org/" ONMOUSEOVER="changeImages('menu_09', 'http://www.jgc.org/images/menu_09-over.gif'); return true;" ONMOUSEOUT="changeImages('menu_09', 'http://www.jgc.org/images/menu_09.gif'); return true;">
  151. <IMG NAME="menu_09" SRC="http://www.jgc.org/images/menu_09.gif" WIDTH=87 HEIGHT=38 BORDER=0 ALT=""></A></td>
  152. <td>
  153. <A HREF="http://blog.jgc.org/" ONMOUSEOVER="changeImages('menu_10', 'http://www.jgc.org/images/menu_10-over.gif'); return true;" ONMOUSEOUT="changeImages('menu_10', 'http://www.jgc.org/images/menu_10-over.gif'); return true;">
  154. <IMG NAME="menu_10" SRC="http://www.jgc.org/images/menu_10-over.gif" WIDTH=71 HEIGHT=38 BORDER=0 ALT=""></A></td>
  155. <td>
  156. <A HREF="http://jgc.org/labs.html" ONMOUSEOVER="changeImages('menu_18', 'http://www.jgc.org/images/menu_18-over.gif'); return true;" ONMOUSEOUT="changeImages('menu_18', 'http://www.jgc.org/images/menu_18.gif'); return true;">
  157. <IMG NAME="menu_18" SRC="http://www.jgc.org/images/menu_18.gif" WIDTH=71 HEIGHT=38 BORDER=0 ALT=""></A></td>
  158. <td>
  159. <A HREF="http://jgc.org/contact.html" ONMOUSEOVER="changeImages('menu_16', 'http://www.jgc.org/images/menu_16-over.gif'); return true;" ONMOUSEOUT="changeImages('menu_16', 'http://www.jgc.org/images/menu_16.gif'); return true;">
  160. <IMG NAME="menu_16" SRC="http://www.jgc.org/images/menu_16.gif" WIDTH=110 HEIGHT=38 BORDER=0 ALT=""></A></td>
  161. </tr>
  162. </table>
  163. </td>
  164. </tr>
  165. <tr>
  166. <td width="100%" style="background-image: url('http://www.jgc.org/images/index_17.gif'); background-repeat: repeat-x; background-position: left top" height="12"></td>
  167. </tr>
  168. <tr valign="top">
  169. <td width="100%" height="100%" valign="top">
  170. <div id="main"><div id="main2">
  171. <h2 class="date-header" class="headerTxt4">Monday, April 30, 2012</h2>
  172. <div class="post" class="headerTxt2"><a name="6665439025357951622"></a>
  173. <h3 class="post-title">
  174. Make your own 'prime factorization' diagram<img src="http://blog.jgc.org/cdn-cgi/cl/197ea504ddebf89f3a5e6bfcb64899d380b1abe4.gif"/>
  175. </h3>
  176. <div class="post-body">
  177. <div>
  178. <div style="clear:both;"></div><div dir="ltr" style="text-align: left;" trbidi="on">
  179. The<a href="http://sonderbooks.com/blog/?p=843"> Prime Factorization Sweater </a>is a lovely idea and I thought it would be fun to reproduce the same idea electronically so that I could print out a poster version for home. <br/>
  180. <br/>
  181. Enter <a href="http://processing.org/">Processing</a>. <br/>
  182. <br/>
  183. With it I've developed a small program that produces a diagram of the first 100 numbers and for each number there's a circle broken up into arcs. &nbsp;Each arc is a prime factor. &nbsp;As in the original sweater each factor gets a unique color (assigning unique colors is rather complex and I ended up using the <a href="http://en.wikipedia.org/wiki/Color_difference#CMC_l:c_.281984.29">color difference method based on CMC l:c</a>&nbsp;and a nice online tool that <a href="http://phrogz.net/css/distinct-colors.html">does the work</a> for you).<br/>
  184. <br/>
  185. Here's the finished product. &nbsp;The top left corner is the number 1 and the numbers read right to left. &nbsp;So the first red circle is a prime number (2), the second the next number (3, which is prime) and so on.<br/>
  186. <br/>
  187. <div class="separator" style="clear: both; text-align: center;">
  188. <a href="http://2.bp.blogspot.com/-hFf6tahQLGg/T56TkfnePFI/AAAAAAAABZ0/M2JpaS2MO2w/s1600/Unnumbered+Primes.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://2.bp.blogspot.com/-hFf6tahQLGg/T56TkfnePFI/AAAAAAAABZ0/M2JpaS2MO2w/s400/Unnumbered+Primes.png" width="400"/></a></div>
  189. <br/>
  190. There's also an option to print the numbers involved.<br/>
  191. <br/>
  192. <div class="separator" style="clear: both; text-align: center;">
  193. <a href="http://4.bp.blogspot.com/-qERJvGYS_6w/T56UZLnH4CI/AAAAAAAABZ8/WZBksofFxWw/s1600/Numbered+Primes.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://4.bp.blogspot.com/-qERJvGYS_6w/T56UZLnH4CI/AAAAAAAABZ8/WZBksofFxWw/s400/Numbered+Primes.png" width="400"/></a></div>
  194. <div class="separator" style="clear: both; text-align: center;">
  195. <br/></div>
  196. <div class="separator" style="clear: both; text-align: left;">
  197. And here's the source code (it's yours under GPLv2):</div>
  198. <pre class="prettyprint" style="font-size: 11pt; line-height: 11pt;">// Prime factorization diagram
  199. //
  200. // Copyright (c) 2012 John Graham-Cumming
  201. //
  202. // Draws a picture of the numbers 1 to 100 as a square.  Each number
  203. // is represented by a circle broken up into arcs of equal size.  Each
  204. // arc represents a prime factor of the number such that all the arcs
  205. // together form the original number.  Each prime is given a unique
  206. // color.
  207. //
  208. // Inspired by the prime factorization sweater:
  209. // http://sonderbooks.com/blog/?p=843
  210.  
  211. // 25 distinct RGB colors generated using the generating service
  212. // on http://phrogz.net/css/distinct-colors.html.  This tries to
  213. // find visually distinct colors that humans will be able to
  214. // distinguish.
  215.  
  216. int[] colors = { #ff0000, #e5b073, #73ff40, #23698c, #312040, #a62929,
  217.   #fff240, #00cc88, #80b3ff, #cc00a3, #e6b4ac, #5c6633, #6cd9d2, #00004d,
  218.   #ff0066, #cc5200, #e1ffbf, #1a3331, #1f00e6, #b3597d, #593000, #338000,
  219.   #00ccff, #986cd9, #401016 };
  220.  
  221. // This is the size of the square on which the prime factorization is
  222. // drawn.  This can be made much larger than the screen for saving a large
  223. // file using saveFrame()
  224.  
  225. int side = 1024;
  226.  
  227. // When set to true the factors are printed inside the circles, otherwise
  228. // just colors are used
  229.  
  230. boolean number = false;
  231.  
  232. // I do everything in setup() because there's no animation and therefore
  233. // no reason to have code in draw()
  234.  
  235. void setup() {
  236.     size(side, side);
  237.     background(255);
  238.     stroke(255);
  239.     strokeWeight(2);
  240.     smooth();
  241.     textAlign(CENTER, CENTER);
  242.  
  243.     int[] primes = new int[100];
  244.     int space = side/10;
  245.     int used = 0;
  246.  
  247.     for (int c = 0; c &lt; 10; c++ ) {
  248.         for (int r = 0; r &lt; 10; r++ ) {
  249.           int n = r+1 + c*10;
  250.           if ( n != 1 ) {
  251.            
  252.             // Factorize the number into its prime factors and determine
  253.             // if it was itself prime.  If so, then assign the next color
  254.             // available to this number for future use.
  255.            
  256.             int[] f = factor(n);
  257.            
  258.             if ( f.length == 1 ) {
  259.               primes[n] = used;
  260.               used += 1;              
  261.             }
  262.            
  263.             float start_angle = PI / 4;
  264.             float angle = start_angle;
  265.             float per_factor = (2 * PI) / f.length;
  266.             float x = space*(r+0.5);
  267.             float y = space*(c+0.5);
  268.            
  269.             // First draw the arcs themselves with the right colors
  270.             // based on the prime factor each represents.  Then draw
  271.             // white lines across the circle that break up the arcs
  272.             // and (if necessary) add the number itself.
  273.            
  274.             for ( int i = 0; i &lt; f.length; i++ ) {
  275.               fill(colors[primes[f[i]]]);
  276.               arc(x, y, space, space, angle, angle+per_factor);
  277.               angle += per_factor;
  278.             }
  279.            
  280.             if ( f.length &gt; 1 ) {
  281.               angle = start_angle;
  282.               for ( int i = 0; i &lt; f.length; i++ ) {
  283.                 line( x, y, x + space/2 * cos(angle), y + space/2.0 * sin(angle) );
  284.                 line( x, y, x + space/2 * cos(angle+per_factor),
  285.                   y + space/2.0 * sin(angle+per_factor) );
  286.                
  287.                 if ( number ) {
  288.                   opposite(colors[primes[f[i]]]);
  289.                   text(f[i], x + space/4*cos(angle+per_factor/2),
  290.                     y + space/4*sin(angle+per_factor/2));
  291.                 }
  292.                
  293.                 angle += per_factor;
  294.               }
  295.             } else {
  296.               if ( number ) {
  297.                 opposite(colors[primes[n]]);
  298.                 text(n, x, y);
  299.               }
  300.             }
  301.           } else {
  302.             if ( number ) {
  303.               opposite(#FFFFFF);
  304.               text(1, space/2, space/2);
  305.             }
  306.           }
  307.         }
  308.     }
  309.    
  310.     saveFrame();
  311. }
  312.  
  313. // factor: return an ordered list (smallest to largest) of the
  314. // prime factors of an integer.
  315. int[] factor(int n) {
  316.   int factors[] = new int[0];
  317.  
  318.   int d = 2;
  319.  
  320.   while (n &gt; 1) {
  321.     while (n % d == 0) {
  322.       factors = append(factors, d);
  323.       n /= d;
  324.     }
  325.     d += 1;
  326.   }
  327.    
  328.   return factors;
  329. }
  330.  
  331. // opposite: set the current fill color to the 'opposite' of
  332. // the passed in color so that it can be seen on top of it. It does this
  333. // by calculating the Euclidean distance of the color from white (255, 255, 255)
  334. // and having a cut off the determines whether to display text in white or black.
  335. void opposite(int c) {
  336.   float distance = sqrt(red(c)*red(c)+green(c)*green(c)+blue(c)*blue(c));
  337.  
  338.   if ( distance &lt; 250 ) {
  339.     fill(255);
  340.   } else {
  341.     fill(0);
  342.   }
  343. }
  344. </pre>
  345. <br/></div><div style="clear:both; padding-bottom:0.25em"></div>
  346. <img src="http://blog.jgc.org/cdn-cgi/cl/197ea504ddebf89f3a5e6bfcb64899d380b1abe4.gif"/>
  347. <p><i>If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: <a href="http://amzn.to/cNCL33">The Geek Atlas</a>. Signed copies of The Geek Atlas are <a href="http://blog.jgc.org/2011/01/how-to-buy-signed-copies-of-geek-atlas.html">available</a>. Looking for a new job? Try <a href="http://jobs.usethesource.com/">UseTheSource</a>.</i></p>
  348. </div>
  349. </div>
  350. <p class="post-footer">
  351. <em>posted by John Graham-Cumming at <a href="http://blog.jgc.org/2012/04/make-your-own-prime-factorization.html" title="permanent link">13:36</a> <a href="http://blog.jgc.org/2012/04/make-your-own-prime-factorization.html" title="permanent link">Permalink</a> </em>
  352. <span class="item-control blog-admin pid-1683558971"><a style="border:none;" href="http://www.blogger.com/post-edit.g?blogID=19303585&postID=6665439025357951622&from=pencil" title="Edit Post"><img class="icon-action" alt="" src="http://img2.blogblog.com/img/icon18_edit_allbkg.gif" height="18" width="18"></a></span>
  353. </p>
  354. </div>
  355. <div id="comments">
  356. <a name="comments"></a>
  357. <h4>0 Comments:</h4>
  358. <dl id="comments-block">
  359. </dl>
  360. <p class="comment-timestamp">
  361. <a class="comment-link" href="http://www.blogger.com/comment.g?blogID=19303585&postID=6665439025357951622">Post a Comment</a>
  362. </p>
  363. <a name="links"></a><h4>Links to this post:</h4>
  364. <dl id="comments-block">
  365. <script type="text/javascript" src="http://www.blogger.com/dyn-js/backlink.js?blogID=19303585&postID=6665439025357951622" charset="utf-8" defer="true">
  366. </script>
  367. <noscript><a href="http://blogsearch.google.com/?ui=blg&q=link:http://blog.jgc.org/2012/04/make-your-own-prime-factorization.html">See links to this post</a></noscript>
  368. <div id="blogger-dcom-block" style="display:none">
  369. <dt class="comment-title">
  370. <span class="comment-toggler">&nbsp;</span>
  371. <a href="<$BlogBacklinkURL$>" rel="nofollow"><$BlogBacklinkTitle$></a> <span class="item-control blog-admin"><a style="border:none;" href="http://www.blogger.com/delete-backlink.g?blogID=19303585&amp;postID=6665439025357951622&amp;backlinkURL=%3C$BlogBacklinkURLEscaped$%3E" title="Remove Link" ><span class="delete-comment-icon">&nbsp;</span></a></span>
  372.         </dt>
  373.         <dd class="comment-body"><$BlogBacklinkSnippet$>
  374.         <br />
  375.         <span class="comment-poster">
  376.         <em>posted by <$BlogBacklinkAuthor$> @ <$BlogBacklinkDateTime$></em>
  377.         </span>
  378.         </dd>
  379.     </div>
  380. <script type="text/javascript">if (typeof BL_addOnLoadEvent == 'function') { BL_addOnLoadEvent(function() { BL_writeBacklinks(); }); }</script>
  381.  
  382.     </dl>
  383.     <p class="comment-timestamp"><script type="text/javascript">function BlogThis() {
  384. Q=''; x=document; y=window;
  385. if(x.selection) {
  386.   Q=x.selection.createRange().text;
  387. } else if (y.getSelection) {
  388.   Q=y.getSelection();
  389. } else if (x.getSelection) {
  390.   Q=x.getSelection();
  391. }
  392. popw = y.open('http://www.blogger.com/blog-this.g?t=' +
  393.   escape(Q) + '&u=' + escape(location.href) + '&n=' +
  394.  escape(document.title),'bloggerForm',
  395.  'scrollbars=no,width=475,height=300,top=175,left=75,status=yes,resizable=yes');
  396. void(0);
  397. }
  398. </script>
  399. <a class="comment-link" href="javascript:BlogThis();" id="b-backlink">Create a Link</a></p>
  400.    
  401.  
  402.  
  403.                 <p class="comment-timestamp">
  404.         <a href="http://blog.jgc.org/">&lt;&lt; Home</a>
  405.     </p>
  406.     </div>
  407.  
  408.  
  409.  
  410.  
  411. </div></div>
  412.  
  413. <div id="sidebar"><div id="sidebar2">
  414. <h2 class="sidebar-title">About</h2>
  415. The troubled thoughts of a caustic coder.  Mostly code, but sometimes rants, randomness and politics.
  416. <h2 class="sidebar-title">Available Now</h2>
  417. <p align="center">
  418. <a href="http://amzn.to/cNCL33">The Geek Atlas</a><br/>
  419. <a href="http://amzn.to/cNCL33"><img src="http://oreilly.com/catalog/covers/9780596523206_cat.gif" width="80px" style="border: 1px solid grey;"></a></p>
  420. With this unique traveler's guide, you'll learn about 128 destinations around the world where discoveries in science, mathematics, or technology occurred or is happening now. Travel to Munich to see the world's largest science museum, watch Foucault's pendulum swinging in Paris, ponder a descendant of Newton's apple tree at Trinity College, Cambridge, and more. Each site in The Geek Atlas focuses on discoveries or inventions, and includes information about the people and the science behind them.
  421.  
  422. <p align="center">
  423. <a href="http://www.lulu.com/content/2584447">GNU Make Unleashed</a><br />
  424. <a href="http://www.lulu.com/content/2584447"><img src="http://www.jgc.org/blog/gnumakeunleashed.png" width="80px" style="border: 1px solid grey;"></a></p>
  425. 230 pages of GNU Make from basics to advanced. Covering topics not covered in other GNU Make books such as: eliminating recursive make, doing arithmetic, Makefile debugging techniques and more.<br/><br/>Everything you wanted to know about making <i>real</i> Makefiles.
  426.  
  427.       <h2 class="sidebar-title">Popular Posts</h2>
  428.  
  429. <div id="nd1328426"></div>
  430. <script src="http://usethesource.com/topwidget.js" type="text/javascript" language="javascript"></script>
  431. <script type="text/javascript" language="javascript">new topwidget("3f4f86d3a5959b63d75e9bf382788c81", "nd1328426", "", null);</script>    
  432.  
  433.       <h2 class="sidebar-title">Recent Posts</h2>
  434.     <ul id="recently">
  435.    
  436.         <li><a href="http://blog.jgc.org/2012/04/tacoli-simple-logging-format.html">tacoli: a simple logging format</a></li>
  437.  
  438.      
  439.         <li><a href="http://blog.jgc.org/2012/04/greatest-machine-that-never-was.html">The Greatest Machine That Never Was</a></li>
  440.  
  441.      
  442.         <li><a href="http://blog.jgc.org/2012/04/simply-user-hostile.html">Simply user hostile</a></li>
  443.  
  444.      
  445.         <li><a href="http://blog.jgc.org/2012/04/deglitching-sparkfun-7-segment-serial.html">Deglitching a Sparkfun 7-segment Serial Display</a></li>
  446.  
  447.      
  448.         <li><a href="http://blog.jgc.org/2012/04/microsoft-is-holding-back-secure-web.html">Microsoft is holding back the secure web</a></li>
  449.  
  450.      
  451.         <li><a href="http://blog.jgc.org/2012/04/getting-around-london-2012-branding.html">Getting around the London 2012 branding police</a></li>
  452.  
  453.      
  454.         <li><a href="http://blog.jgc.org/2012/04/phillippe-starck-impractical-designer.html">Philippe Starck: the impractical designer</a></li>
  455.  
  456.      
  457.         <li><a href="http://blog.jgc.org/2012/04/more-support-for-open-software-in.html">More support for open software in science</a></li>
  458.  
  459.      
  460.         <li><a href="http://blog.jgc.org/2012/04/brief-plan-28-update.html">Brief Plan 28 Update</a></li>
  461.  
  462.      
  463.         <li><a href="http://blog.jgc.org/2012/04/bletchley-park-is-blooming.html">Bletchley Park is Blooming</a></li>
  464.  
  465.      
  466.   </ul>
  467.    
  468.  
  469.         <p id="blogfeeds">Subscribe to<br />Posts [<a target="_blank" href="http://blog.jgc.org/feeds/posts/default" type="application/atom+xml">Atom</a>]</p>
  470.  
  471. </div></div>
  472.  
  473.   </td>
  474.  
  475.   </tr>
  476. </table>
  477. </div>
  478.  
  479. <div id="footer">
  480.  
  481. <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  482.   <tr>
  483.     <td width="100%" style="background-image: url('http://www.jgc.org/images/index_20.gif'); background-repeat: repeat-x; background-position: left top" height="12"></td>
  484.   </tr>
  485.   <tr>
  486.     <td width="100%">
  487.     <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber4">
  488.       <tr>
  489.  
  490.         <td>
  491.                         <IMG SRC="http://www.jgc.org/images/index_22.gif" WIDTH=12 HEIGHT=25 ALT=""></td>
  492.         <td style="background-image: url('http://www.jgc.org/images/index_23.gif'); background-repeat: repeat-x; background-position: left top" width="100%" class="copyrightTxt" align="center">
  493.         Copyright (c) 1999-2012 John Graham-Cumming</td>
  494.         <td>
  495.                         <IMG SRC="http://www.jgc.org/images/index_25.gif" WIDTH=11 HEIGHT=25 ALT=""></td>
  496.       </tr>
  497.     </table>
  498.  
  499.     </td>
  500.   </tr>
  501.   <tr>
  502.     <td width="100%" style="background-image: url('http://www.jgc.org/images/index_26.gif'); background-repeat: repeat-x; background-position: left top" height="21">&nbsp;</td>
  503.   </tr>
  504. </table>
  505. <img src="http://l8tr.org/uptime">
  506. <script type="text/javascript">
  507. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  508. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  509. </script>
  510. <script type="text/javascript">
  511. try {
  512. var pageTracker = _gat._getTracker("UA-402747-4");
  513. pageTracker._trackPageview();
  514. } catch(err) {}</script>
  515. <script type="text/javascript">
  516. var _sf_async_config={uid:13223,domain:"blog.jgc.org"};
  517. (function(){
  518.   function loadChartbeat() {
  519.     window._sf_endpt=(new Date()).getTime();
  520.     var e = document.createElement('script');
  521.     e.setAttribute('language', 'javascript');
  522.     e.setAttribute('type', 'text/javascript');
  523.     e.setAttribute('src',
  524.        (("https:" == document.location.protocol) ? "https://s3.amazonaws.com/" : "http://") +
  525.        "static.chartbeat.com/js/chartbeat.js");
  526.     document.body.appendChild(e);
  527.   }
  528.   var oldonload = window.onload;
  529.   window.onload = (typeof window.onload != 'function') ?
  530.      loadChartbeat : function() { oldonload(); loadChartbeat(); };
  531. })();
  532.  
  533. </script>
  534. <span style="display: none;">eaca7fba4d52e790ad972d6a72844b5722d94bbd</span>
  535. <script type="text/javascript" src="http://www.blogger.com/static/v1/common/js/612699025-csitail.js"></script>
  536. <script type="text/javascript">BLOG_initCsi('classic_blogspot');</script><script type="text/javascript">
  537. //<![CDATA[
  538. window.__CF=window.__CF||{};window.__CF.corn='k';(function(a,b){var c="__cornify_nodes",d=a.__CF||{},e=d.cornifyExists||!!b.getElementById(c),f=d.corn,g=function(){(f=="a"||f=="r"&&Math.random()<.1)&&setTimeout(function(){cornify_add()},2e3)};if(e)g();else{var h=b.createElement("script"),i=b.getElementsByTagName("script")[0];h.src="http://www.cornify.com/js/cornify.js",h.async=!0,h.type="text/javascript",h.onload=h.onreadystatechange=function(){var a=h.readyState;(!a||a=="loaded"||a=="complete")&&g()},i.parentNode.insertBefore(h,i)}})(window,document);
  539. //]]>
  540. </script></body>
  541. * Connection #0 to host blog.jgc.org left intact
  542. * Closing connection #0