· 8 years ago · Jun 04, 2018, 05:22 PM
1documentclass{article}
2usepackage{tikz}
3begin{document}
4begin{tikzpicture}[scale=0.65]
5draw[very thick,red] (3,9)--(4,10);
6draw[very thick,red] (3,10)--(4,9);
7draw[very thick,red] (5,9)--(6,10);
8draw[very thick,red] (5,10)--(6,9);
9draw[very thick,red] (7,9)--(8,10);
10draw[very thick,red] (7,10)--(8,9);
11draw[very thick,red] (8,9)--(9,10);
12draw[very thick,red] (8,10)--(9,9);
13draw[very thick,red] (9,9)--(10,10);
14draw[very thick,red] (9,10)--(10,9);
15%------------------------------------
16draw[very thick,red] (1,8)--(2,9);
17draw[very thick,red] (2,8)--(1,9);
18draw[very thick,red] (3,8)--(4,9);
19draw[very thick,red] (4,8)--(3,9);
20draw[very thick,red] (4,8)--(5,9);
21draw[very thick,red] (5,8)--(4,9);
22draw[very thick,red] (5,8)--(6,9);
23draw[very thick,red] (6,8)--(5,9);
24draw[very thick,red] (7,8)--(8,9);
25draw[very thick,red] (8,8)--(7,9);
26draw[very thick,red] (9,8)--(10,9);
27draw[very thick,red] (10,8)--(9,9);
28%------------------------------------
29draw[very thick,red] (0,7)--(1,8);
30draw[very thick,red] (1,7)--(0,8);
31draw[very thick,red] (1,7)--(2,8);
32draw[very thick,red] (2,7)--(1,8);
33draw[very thick,red] (3,7)--(4,8);
34draw[very thick,red] (4,7)--(3,8);
35draw[very thick,red] (4,7)--(5,8);
36draw[very thick,red] (5,7)--(4,8);
37draw[very thick,red] (5,7)--(6,8);
38draw[very thick,red] (6,7)--(5,8);
39draw[very thick,red] (6,7)--(7,8);
40draw[very thick,red] (7,7)--(6,8);
41draw[very thick,red] (7,7)--(8,8);
42draw[very thick,red] (8,7)--(7,8);
43draw[very thick,red] (9,7)--(10,8);
44draw[very thick,red] (10,7)--(9,8);
45%------------------------------------
46draw[very thick,red] (1,6)--(2,7);
47draw[very thick,red] (2,6)--(1,7);
48draw[very thick,red] (2,6)--(3,7);
49draw[very thick,red] (3,6)--(2,7);
50draw[very thick,red] (3,6)--(4,7);
51draw[very thick,red] (4,6)--(3,7);
52draw[very thick,red] (4,6)--(5,7);
53draw[very thick,red] (5,6)--(4,7);
54draw[very thick,red] (5,6)--(6,7);
55draw[very thick,red] (6,6)--(5,7);
56draw[very thick,red] (7,6)--(8,7);
57draw[very thick,red] (8,6)--(7,7);
58draw[very thick,red] (8,6)--(9,7);
59draw[very thick,red] (9,6)--(8,7);
60draw[very thick,red] (9,6)--(10,7);
61draw[very thick,red] (10,6)--(9,7);
62%------------------------------------
63draw[very thick,red] (1,5)--(2,6);
64draw[very thick,red] (2,5)--(1,6);
65draw[very thick,red] (3,5)--(4,6);
66draw[very thick,red] (4,5)--(3,6);
67draw[very thick,red] (4,5)--(5,6);
68draw[very thick,red] (5,5)--(4,6);
69draw[very thick,red] (5,5)--(6,6);
70draw[very thick,red] (6,5)--(5,6);
71draw[very thick,red] (7,5)--(8,6);
72draw[very thick,red] (8,5)--(7,6);
73draw[very thick,red] (8,5)--(9,6);
74draw[very thick,red] (9,5)--(8,6);
75draw[very thick,red] (9,5)--(10,6);
76draw[very thick,red] (10,5)--(9,6);
77%------------------------------------
78draw[very thick,red] (0,4)--(1,5);
79draw[very thick,red] (1,4)--(0,5);
80draw[very thick,red] (1,4)--(2,5);
81draw[very thick,red] (2,4)--(1,5);
82draw[very thick,red] (3,4)--(4,5);
83draw[very thick,red] (4,4)--(3,5);
84draw[very thick,red] (4,4)--(5,5);
85draw[very thick,red] (5,4)--(4,5);
86draw[very thick,red] (5,4)--(6,5);
87draw[very thick,red] (6,4)--(5,5);
88draw[very thick,red] (6,4)--(7,5);
89draw[very thick,red] (7,4)--(6,5);
90draw[very thick,red] (7,4)--(8,5);
91draw[very thick,red] (8,4)--(7,5);
92draw[very thick,red] (9,4)--(10,5);
93draw[very thick,red] (10,4)--(9,5);
94%------------------------------------
95draw[very thick,red] (1,3)--(2,4);
96draw[very thick,red] (2,3)--(1,4);
97draw[very thick,red] (2,3)--(3,4);
98draw[very thick,red] (3,3)--(2,4);
99draw[very thick,red] (3,3)--(4,4);
100draw[very thick,red] (4,3)--(3,4);
101draw[very thick,red] (4,3)--(5,4);
102draw[very thick,red] (5,3)--(4,4);
103draw[very thick,red] (5,3)--(6,4);
104draw[very thick,red] (6,3)--(5,4);
105draw[very thick,red] (7,3)--(8,4);
106draw[very thick,red] (8,3)--(7,4);
107draw[very thick,red] (8,3)--(9,4);
108draw[very thick,red] (9,3)--(8,4);
109draw[very thick,red] (9,3)--(10,4);
110draw[very thick,red] (10,3)--(9,4);
111%------------------------------------
112draw[very thick,red] (1,2)--(2,3);
113draw[very thick,red] (2,2)--(1,3);
114draw[very thick,red] (3,2)--(4,3);
115draw[very thick,red] (4,2)--(3,3);
116draw[very thick,red] (4,2)--(5,3);
117draw[very thick,red] (5,2)--(4,3);
118draw[very thick,red] (5,2)--(6,3);
119draw[very thick,red] (6,2)--(5,3);
120draw[very thick,red] (6,2)--(7,3);
121draw[very thick,red] (7,2)--(6,3);
122draw[very thick,red] (7,2)--(8,3);
123draw[very thick,red] (8,2)--(7,3);
124draw[very thick,red] (9,2)--(10,3);
125draw[very thick,red] (10,2)--(9,3);
126%------------------------------------
127draw[very thick,red] (0,1)--(1,2);
128draw[very thick,red] (1,1)--(0,2);
129draw[very thick,red] (1,1)--(2,2);
130draw[very thick,red] (2,1)--(1,2);
131draw[very thick,red] (3,1)--(4,2);
132draw[very thick,red] (4,1)--(3,2);
133draw[very thick,red] (4,1)--(5,2);
134draw[very thick,red] (5,1)--(4,2);
135draw[very thick,red] (5,1)--(6,2);
136draw[very thick,red] (6,1)--(5,2);
137draw[very thick,red] (6,1)--(7,2);
138draw[very thick,red] (7,1)--(6,2);
139draw[very thick,red] (7,1)--(8,2);
140draw[very thick,red] (8,1)--(7,2);
141draw[very thick,red] (9,1)--(10,2);
142draw[very thick,red] (10,1)--(9,2);
143%------------------------------------
144draw[very thick,red] (0,0)--(1,1);
145draw[very thick,red] (1,0)--(0,1);
146draw[very thick,red] (1,0)--(2,1);
147draw[very thick,red] (2,0)--(1,1);
148draw[very thick,red] (2,0)--(3,1);
149draw[very thick,red] (3,0)--(2,1);
150draw[very thick,red] (3,0)--(4,1);
151draw[very thick,red] (4,0)--(3,1);
152draw[very thick,red] (4,0)--(5,1);
153draw[very thick,red] (5,0)--(4,1);
154draw[very thick,red] (5,0)--(6,1);
155draw[very thick,red] (6,0)--(5,1);
156draw[very thick,red] (7,0)--(8,1);
157draw[very thick,red] (8,0)--(7,1);
158draw[very thick,red] (8,0)--(9,1);
159draw[very thick,red] (9,0)--(8,1);
160draw[very thick,red] (9,0)--(10,1);
161draw[very thick,red] (10,0)--(9,1);
162%------------------------------------
163draw (0,0) grid (10,10);
164draw (0.5,9.5) node {};
165draw (1.5,9.5) node {2};
166draw (2.5,9.5) node {3};
167draw (3.5,9.5) node {4};
168draw (4.5,9.5) node {5};
169draw (5.5,9.5) node {6};
170draw (6.5,9.5) node {7};
171draw (7.5,9.5) node {8};
172draw (8.5,9.5) node {9};
173draw (9.5,9.5) node {10};
174
175%--------------------------
176draw (0.5,8.5) node {11};
177draw (1.5,8.5) node {12};
178draw (2.5,8.5) node {13};
179draw (3.5,8.5) node {14};
180draw (4.5,8.5) node {15};
181draw (5.5,8.5) node {16};
182draw (6.5,8.5) node {17};
183draw (7.5,8.5) node {18};
184draw (8.5,8.5) node {19};
185draw (9.5,8.5) node {20};
186%--------------------------
187draw (0.5,7.5) node {21};
188draw (1.5,7.5) node {22};
189draw (2.5,7.5) node {23};
190draw (3.5,7.5) node {24};
191draw (4.5,7.5) node {25};
192draw (5.5,7.5) node {26};
193draw (6.5,7.5) node {27};
194draw (7.5,7.5) node {28};
195draw (8.5,7.5) node {29};
196draw (9.5,7.5) node {30};
197%--------------------------
198draw (0.5,6.5) node {31};
199draw (1.5,6.5) node {32};
200draw (2.5,6.5) node {33};
201draw (3.5,6.5) node {34};
202draw (4.5,6.5) node {35};
203draw (5.5,6.5) node {36};
204draw (6.5,6.5) node {37};
205draw (7.5,6.5) node {38};
206draw (8.5,6.5) node {39};
207draw (9.5,6.5) node {40};
208%--------------------------
209draw (0.5,5.5) node {41};
210draw (1.5,5.5) node {42};
211draw (2.5,5.5) node {43};
212draw (3.5,5.5) node {44};
213draw (4.5,5.5) node {45};
214draw (5.5,5.5) node {46};
215draw (6.5,5.5) node {47};
216draw (7.5,5.5) node {48};
217draw (8.5,5.5) node {49};
218draw (9.5,5.5) node {50};
219%--------------------------
220draw (0.5,4.5) node {51};
221draw (1.5,4.5) node {52};
222draw (2.5,4.5) node {53};
223draw (3.5,4.5) node {54};
224draw (4.5,4.5) node {55};
225draw (5.5,4.5) node {56};
226draw (6.5,4.5) node {57};
227draw (7.5,4.5) node {58};
228draw (8.5,4.5) node {59};
229draw (9.5,4.5) node {60};
230%--------------------------
231draw (0.5,3.5) node {61};
232draw (1.5,3.5) node {62};
233draw (2.5,3.5) node {63};
234draw (3.5,3.5) node {64};
235draw (4.5,3.5) node {65};
236draw (5.5,3.5) node {66};
237draw (6.5,3.5) node {67};
238draw (7.5,3.5) node {68};
239draw (8.5,3.5) node {69};
240draw (9.5,3.5) node {60};
241%--------------------------
242draw (0.5,2.5) node {71};
243draw (1.5,2.5) node {72};
244draw (2.5,2.5) node {73};
245draw (3.5,2.5) node {74};
246draw (4.5,2.5) node {75};
247draw (5.5,2.5) node {76};
248draw (6.5,2.5) node {77};
249draw (7.5,2.5) node {78};
250draw (8.5,2.5) node {79};
251draw (9.5,2.5) node {80};
252%--------------------------
253draw (0.5,1.5) node {81};
254draw (1.5,1.5) node {82};
255draw (2.5,1.5) node {83};
256draw (3.5,1.5) node {84};
257draw (4.5,1.5) node {85};
258draw (5.5,1.5) node {86};
259draw (6.5,1.5) node {87};
260draw (7.5,1.5) node {88};
261draw (8.5,1.5) node {89};
262draw (9.5,1.5) node {90};
263%--------------------------
264draw (0.5,0.5) node {91};
265draw (1.5,0.5) node {92};
266draw (2.5,0.5) node {93};
267draw (3.5,0.5) node {94};
268draw (4.5,0.5) node {95};
269draw (5.5,0.5) node {96};
270draw (6.5,0.5) node {97};
271draw (7.5,0.5) node {98};
272draw (8.5,0.5) node {99};
273draw (9.5,0.5) node {100};
274end{tikzpicture}
275end{document}
276
277%defShowStepByStep{}% Ignored in this case
278defAnimateSieve{}%
279%defAnimatedGif{}% Must be commented
280
281defNumOfColumns{10}%
282defNumOfRows{10}%
283defFramesToHoldAtEnd{25}% 25 is enough for 10x10
284
285%defShowStepByStep{}% Ignored in this case
286%defAnimateSieve{}% Ignored in this case
287defAnimatedGif{}%
288defNumOfColumns{5}%
289defNumOfRows{5}%
290defFramesToHoldAtEnd{10}%
291
292pdfcrop SieveOfEratosthenesAnimated.pdf
293convert -verbose -delay 100 -loop 0 -density 400 SieveOfEratosthenesAnimated-crop.pdf SieveOfEratosthenesAnimated.gif
294
295defShowStepByStep{}%
296%defAnimateSieve{}% This MUST be commented
297%defAnimatedGif{}% This MUST be commented
298defNumOfColumns{7}%
299defNumOfRows{7}%
300defFramesToHoldAtEnd{10}%
301
302%defShowStepByStep{}% ALL of these must be commented
303%defAnimateSieve{}%
304%defAnimatedGif{}%
305defNumOfColumns{10}%
306defNumOfRows{10}%
307defFramesToHoldAtEnd{25}% 25 is enough for 10x10
308
309%%% Sieve of Eratosthenes
310%%% ---------------------
311%%%
312%%% Step 1: Create a list of consecutive integers 2...n
313%%% Step 2: Let p=2 be the first prime number
314%%% Step 3: Mark multiples of p as non-prime.
315%%% Step 4: First number > p not marked as non-prime is prime.
316%%% Step 5: Repeat from Step 3 with this new prime.
317%%%
318
319%%% ---------------------------------------------------------------
320%%% Set-up: Choose desired output:
321%%% 1. Animation: GIF, or PDF
322%%% 2. Paper version: Step by step) or Final state.
323%%%
324
325%defShowStepByStep{}% Comment out if only want final result
326
327%%% Choose if want animated version. This overrides ShowStepByStep
328defAnimateSieve{}% Comment out if don't want animated version
329
330%%% Chose if want animated gif image instead. Overrides AnimateSieve
331%defAnimatedGif{}%
332%
333%%% The AnimatedGif option produces a PDF with each page containing
334%%% a single frame. To convert this to a GIF, use the following, where
335%%% convert is part of ImageMagik
336%%%
337%%% pdfcrop SieveOfEratosthenes.pdf
338%%% convert -verbose -delay 100 -loop 0 -density 400
339%%% SieveOfEratosthenes-crop.pdf
340%%% SieveOfEratosthenes.gif
341
342
343
344%%% ---------------------------------------------------------------
345%%% Customize: Choose size: NumberOfColumns x NumberOfRows
346%%% Other options may need tweaking based on size settings
347%%%
348%%% Note that if the product of NumOfColumns x NumOfRows
349%%% is greater than 100, the FramesToHoldAtEnd should be
350%%% larger than 25. No check below is made of this, but will
351%%% result in some of larger primes not being highlighted at
352%%% the end of the cycle (if this is not large enough).
353
354defNumOfColumns{10}% See note above if product of
355defNumOfRows{10}% NumOfColumns and NumOfRows > 100.
356
357%% FramesToHoldAtEnd should be larger than the number of primes
358%% so that they can get highlighted at the end of the process
359defFrameRate{1}%
360defFramesToHoldAtStart{3}%
361defFramesToHoldAtEnd{25}% 25 is enough for 10x10
362
363
364defScale{0.6}% May need tweaking..
365
366defMinipageScale{1.0}%
367defMinipageScaleForStepByStep{0.49}%
368
369%% Without this scale adjustment for the animated GIF, the image is quite large.
370defScaleForAnimatedGif{0.6}%
371
372
373defPrimeColor{yellow}% Shade for primes found previously
374defNewPrimeColor{cyan}% Shade for prime just found
375defNewPrimeText{blue}% Color for primes in list
376defNonPrimeColor{red}% Shade for non-primes
377
378
379%% List of Primes is typeset into a node of this width.
380defTextWidth{2.0cm}%
381
382
383%%% ---------------------------------------------------------------
384ifdefinedAnimatedGif
385 documentclass[border=2pt,multi=true]{standalone}
386elseifdefinedAnimateSieve
387 documentclass{article}
388else
389 documentclass{article}% for paper version
390fifi
391
392%%% Can use the following to show the final state for large
393%%% (tested up to 53x52)
394%usepackage[paperwidth=35in,paperheight=35in]{geometry}
395usepackage{geometry}
396
397usepackage{microtype}% Allow comma into margin in list of primes
398usepackage{xstring}% String comparison
399usepackage{tikz}% Drawing
400usetikzlibrary{calc}% Coordinate calculations
401usetikzlibrary{backgrounds}% Apply shading on background layer
402
403ifdefinedAnimatedGif
404 usepackage{animate}% no controls, or looping needed
405 defAnimateSieve{}% Simplifies code if this is set for AnimatedGif as well.
406 defScale{ScaleForAnimatedGif}% Otherwise GIF is too large
407
408 % Simplifies code below if we just redefine these two from the
409 % animate package so that they do don't much.
410 renewenvironment{animateinline}[1]{begingroup}{endgroup}%
411 renewcommand{newframe}[1][]{newpage}%
412else% Note: This else is skipped for AnimatedGif
413 ifdefinedAnimateSieve%
414 usepackage[loop,controls]{animate}% looped animation
415 letShowStepBySteprelax% Ensure that ShowStepByStep is undefined
416 else% Print version
417 usepackage{animate}% provides whiledo (could include ifthen)
418
419 % Simplifies code below if we just redefine these two from the
420 % animate package so that they do don't much.
421 renewenvironment{animateinline}[1]{begingroup}{endgroup}%
422 renewcommand{newframe}[1][]{newpage}%
423
424 ifdefinedShowStepByStep
425 defMinipageScale{MinipageScaleForStepByStep}%
426 fi
427 fi
428fi
429
430
431
432
433%%% ---------------------------------------------------------------
434%%% Should not need to adjust anything below this line
435%%%
436pgfmathtruncatemacro{MaxNumber}{NumOfRows*NumOfColumns}%
437pgfmathtruncatemacro{MaxValue}{sqrt(MaxNumber)}%
438
439% Choose opacity so that we can have the max number of shades
440pgfmathsetmacro{Opacity}{1.0/min(20,MaxValue-1)}%
441
442%% The Sieve algorithm requires that once a number is marked
443%% as non-prime (i.e., was a multiple of some other number)
444%% we don't need to check multiples of that number as they
445%% have already been marked as non-prime.
446%%
447%% Usually one would use an array and set a flag. But since
448%% variables with numbers are difficult with TeX, we can
449%% define a node named with the number that is non-prime.
450%% Then just check that the node exists to see if it was
451%% marked as non-prime.
452
453makeatletter
454% Mark number as either "Prime" or "NonPrime".
455newcommand*{MarkNumber}[2][NonPrime]{node (#1#2) {}}% #1=prefix, #2=num
456
457% https://tex.stackexchange.com/questions/37709/how-can-i-know-if-a-node-is-already-defined
458newcommand{IfNumberAlreadyMarked}[4][NonPrime]{% #1=prefix, #2=num
459 pgfutil@ifundefined{pgf@sh@ns@#1#2}{#4}{#3}%
460}
461
462% https://tex.stackexchange.com/questions/20655/how-to-undo-a-def-i-e-need-a-undef-capability
463newcommand*@nameundef[1]{%
464 globalexpandafterletcsname #1endcsname@undefined%
465}
466
467%% Since we repeat the process from the beginning for the animated
468%% version, use this to clear the nodes so that the numbers are
469%% not marked as multiples of a number from the previous run.
470newcommand{ClearAllNumberedNodeNames}{%
471 foreach i in {1,...,MaxValue}{%
472 @nameundef{pgf@sh@ns@NonPrimei}%
473 @nameundef{pgf@sh@ns@Primei}%
474 }%
475}
476makeatother
477
478%% The Sieve algorithm skips multiples of numbers already marked as
479%% non-prime. So, to number the individual steps, need to use
480%% a counter.
481%% i.e., Step 4 is processing multiples of 5 (since we skip 4).
482newcounter{StepNumber}%
483
484
485%%% ---------------------------------------------------------------
486%%%
487%%% Titles and Labels
488%%%
489
490newcommandListOfPrimes{}
491newcommandAddToListOfPrimes[2][fill=PrimeColor]{%
492 IfStrEq{ListOfPrimes}{}{%
493 defSeparator{}% First member of list of primes
494 }{%
495 defSeparator{, }% Subsequent member of list of primes
496 }%
497 %
498 FillCellForGivenNumber[#1]{#2};%
499 globaledefListOfPrimes{ListOfPrimesSeparator#2}%
500 MarkNumber[Prime]{#2};%
501}
502
503newcommand*{ClearListOfPrimes}{%
504 ClearAllNumberedNodeNames;%
505 renewcommand{ListOfPrimes}{}%
506}
507
508
509newcommand*{Title}{%
510 {noindentLarge%
511 textbf{Sieve of Eratosthenes}~%
512 ($NumOfColumns times NumOfRows$)%
513 }%
514}
515
516newcommand*{SubTitleInitial}{%
517 noindenttextbf{Step theStepNumber}: Numbers from 2 ldotsMaxNumber%
518}%
519
520newcommand*{SubTitle}[1]{% For animation
521 noindenttextbf{Step theStepNumber}:~%
522 Eliminating multiples of textcolor{NewPrimeText}{textbf{#1}}%
523}
524newcommand*{SubTitlePastTense}[1]{% For step by step
525 noindenttextbf{Step theStepNumber}:~%
526 Eliminated multiples of textcolor{NewPrimeText}{textbf{#1}}%
527}
528newcommand*{SubTitleFinal}{%
529 IfEq{thevalue{StepNumber}}{0}{%
530 % We are only showing the final result, so no steps to label.
531 % This is when we are not animating (nor showing step by step)
532 }{%
533 noindenttextbf{Step theStepNumber}: Remaining are prime.%
534 }%
535}
536
537
538newcommand*{AddTitleNode}{%
539 ifdefinedAnimateSieve% Otherwise don't need title each time
540 node [above, yshift=1.0ex] at ($(0,0)!0.5!(NumOfColumns,0)$) {Title}
541 fi%
542}
543
544newcommand*{AddSubTitleNode}[1]{%
545 IfStrEq{#1}{empty}{%
546 % This is the final hold frame where we are showing the primes
547 node [right] at (-1,0) {SubTitleFinal}
548 }{%
549 ifdefinedAnimateSieve%
550 node at ($(0,0)!0.5!(NumOfColumns,0)$) {SubTitle{#1}}
551 else%
552 node [right] at (-1,0) {SubTitlePastTense{#1}}
553 fi%
554 }%
555}
556
557newcommand*{AddInitialSubTitleNode}[1]{%
558 ifdefinedAnimateSieve%
559 node at ($(0,0)!0.5!(NumOfColumns,0)$) {SubTitleInitial}
560 else%
561 node [right] at (-1,0) {SubTitleInitial}
562 fi%
563}
564
565
566newcommand*{Phantom}[1]{}%
567newcommand*{ShowListOfPrimesNode}{%
568 IfStrEq{ListOfPrimes}{}{%
569 %% Empty list of primes, so don't want to show anything.
570 %% Just add phantom spacing
571 renewcommand*{Phantom}[1]{phantom{##1}}%
572 }{%
573 renewcommand*{Phantom}[1]{##1}%
574 }%
575
576 node [below right, xshift=0.5em, yshift=-0.5ex, align=left, text width=TextWidth]
577 at (NumOfColumns,-1)
578 {Phantom{textbf{Primes:}}};
579
580 node [below right, xshift=0.2em, yshift=-3.5ex, align=left, text width=TextWidth]
581 at (NumOfColumns,-1)
582 {Phantom{textbf{textcolor{NewPrimeText}{raggedleftListOfPrimes}}}};
583}
584
585%%% ---------------------------------------------------------------
586
587%%%
588%%% Step 1: Create a list of integers 2...n
589%%%
590newcommand*{DrawGridWithNumbers}{%
591 begin{scope}[draw=gray, thick]% Add numbers to each node
592 draw (0,-1) -- ($(0,-NumOfRows-1)$);
593 foreach col in {1,...,NumOfColumns} {%
594 draw (col,-1) -- ($(col,-NumOfRows-1)$);
595
596 draw (0,-1) -- (NumOfColumns,-1);
597 foreach row in {1,...,NumOfRows}{%
598 pgfmathtruncatemacro{value}{col+NumOfColumns*(row-1)}
599 IfEq{value}{1}{
600 %% Suppress number 1 from being printed since first
601 %% step of Sieve of Eratosthenes algorithm is to
602 %% create a list of integers 2...n
603 }{
604 node at ($(col,-row)-(0.5,0.5)$) {textbf{value}};
605 }
606 draw (0,-row-1) -- (NumOfColumns,-row-1);
607 }
608 }
609 end{scope}
610
611 %% Since we just drew the grid we should ensure that none
612 %% of the numbered nodes exist (i.e., that no numbers
613 %% are marked as non-prime. And reset list of primes.
614
615 ClearListOfPrimes;
616 ClearAllNumberedNodeNames;
617
618 ShowListOfPrimesNode;
619}
620
621newcommand*{FillCellForGivenNumber}[2][]{%
622 %% #1 = fill options
623 %% #2 = number
624 %%
625 pgfmathtruncatemacro{Column}{mod(#2,NumOfColumns)}%
626 IfEq{Column}{0}{pgfmathtruncatemacro{Column}{NumOfColumns}}{}%
627 pgfmathtruncatemacro{Row}{(#2-1)/NumOfColumns+1}%
628
629 begin{scope}[on background layer]
630 fill [#1]
631 (Column-1,-Row) --
632 ($(Column-1,-Row)+(1,0)$) --
633 ($(Column-1,-Row)+(1,-1)$) --
634 (Column-1,-Row-1) --
635 cycle;
636 end{scope}
637}
638
639
640
641newcommand*{ColorMultiplesOf}[2][0]{%
642 %% If only 1 arg is given (i.e., #1=0), then
643 %% #2 = the multiple for which the coloring is applied
644 %%
645 %% If two args are given (i.e., #1 != 0) then
646 %% #1 = Value of MaxMultiple (used for animated version)
647 %% In the two arg case we run the entire sequence
648 %% from the beginning up until the multiple #1*#2
649 %% is reached.
650
651 IfEq{#1}{0}{% Run the entire sequence
652 pgfmathtruncatemacro{MaxMultiple}{MaxNumber/#2}
653 }{% Run sequence up until number given for animating
654 defMaxMultiple{#1}
655 }
656
657 foreach i in {2,...,MaxMultiple} {
658 pgfmathtruncatemacro{NonPrimeNumber}{i*#2}
659 FillCellForGivenNumber[
660 fill=NonPrimeColor,
661 fill opacity=Opacity
662 ]
663 {NonPrimeNumber};
664 MarkNumber[NonPrime]{NonPrimeNumber};
665 }
666}
667
668
669newcommand*{BuildFrameInternals}[2][0]{%
670 %% #1 = current multiple to which to build the pattern up to
671 %% if #1=0 and #2=MaxValue, then we are in an end hold frame
672 %% #2 = number of whose multiples we are eliminating in this step
673 %% if #2=1, then only draw grid (provides hold frame at start)
674
675 AddTitleNode;% Print Main title if AnimateSieve is defined
676
677 DrawGridWithNumbers;
678 IfEq{#2}{1}{%
679 %% This is a hold frame at start so only show grid of numbers
680 AddInitialSubTitleNode{#2};
681 }{%
682 IfEq{#2}{2}{%
683 %% No pre-processing steps to be done in this case
684 }{%
685 %% Since we are eliminating multiples of a number
686 %% other than 2, we need to get the table up to
687 %% the state where all the multiples of 2...(#2-1)
688 %% are eliminated.
689
690 pgfmathsetmacro{PreviousMultiple}{#2 - 1}%
691 foreach n in {2,...,PreviousMultiple} {%
692 IfNumberAlreadyMarked[NonPrime]{n}{%
693 %% Skip. Multiples are already marked as non-prime
694 %% since this number is a multiple of a smaller
695 %% prime.
696 }{%
697 %% This is a prime. Mark it as prime, and mark
698 %% its multiples as non-prime.
699 AddToListOfPrimes[fill=PrimeColor]{n};
700 ColorMultiplesOf{n};
701 }
702 }
703 }
704
705 IfNumberAlreadyMarked[NonPrime]{#2}{%
706 %% Already taken care of in a previous run. This test
707 %% is needed to cover the case where the value of the
708 %% sqrt{NumberOfColumns x NumberOfRows) is not prime.
709 %% For example: 10x10.
710 }{%
711 %% Now eliminate the numbers up to the current state
712 AddToListOfPrimes[fill=NewPrimeColor]{#2};
713 ColorMultiplesOf[#1]{#2};
714 }
715
716 %% If we are holding the very final result don't print title.
717 %% This is the case when #2=MaxValue and #1=0.
718 %%
719 %% Need to do this at the end so that we can access
720 %% which numbers have been marked as non-prime.
721
722 IfEq{#2}{MaxValue}{%
723 IfEq{#1}{0}{%
724 %% This is the final hold frame
725 SubTitleFinal;
726
727 IfNumberAlreadyMarked[NonPrime]{#2}{%
728 }{%
729 IfNumberAlreadyMarked[Prime]{#2}{%
730 %% In this case, #2 is not a new prime so
731 %% correct its color. So, don't add it to the
732 %% list of primes, but correct ensure its
733 %% color corresponds to an old prime
734 FillCellForGivenNumber[fill=PrimeColor]{#2};
735 }{%
736 %% In this case, #2 is a new prime so
737 %% add it to the list of primes,
738 AddToListOfPrimes[fill=NewPrimeColor]{#2};
739 }%
740 }%
741
742 %% But since this is the final hold frame, we need
743 %% to mark all the numbers not already marked as
744 %% non-prime as prime. Do one at at time, so that
745 %% this can be seen in the animation.
746
747 pgfmathtruncatemacro{StartValue}{MaxValue+1}%
748 foreach p in {StartValue,...,MaxNumber}{%
749 IfNumberAlreadyMarked[NonPrime]{p}{%
750 %% This number has been marked as non-prime
751 }{%
752 %% This is a prime
753 IfNumberAlreadyMarked[Prime]{p}{%
754 %% Already found this prime earlier.
755 %% So ensure it has appropriate fill.
756 AddToListOfPrimes[fill=PrimeColor]{p};%
757 }{%
758 %% New prime: Mark it as such, and
759 %% break out to complete this frame.
760 AddToListOfPrimes[fill=NewPrimeColor]{p};%
761 MarkNumber[Prime]{p};%
762 AddSubTitleNode{};%
763 breakforeach;%
764 }%
765 }%
766 }%
767 }{%
768 %% Not final hold frame, so normal title
769 AddSubTitleNode{#2};%
770 }%
771 }{%
772 AddSubTitleNode{#2};%
773 }%
774 }%
775 ShowListOfPrimesNode%
776}%
777
778newcommand*{AddVerticalSpearationForStepByStep}{%
779 ifdefinedShowStepByStep% So that the minipages for this case
780 vspace*{4.0ex}% are not stacked directly on top of
781 fi% each other.
782}%
783
784newcommand*{BuildFrame}[2][0]{%
785 %% #1 = current multiple to which to build the pattern up to
786 %% #2 = number of whose multiples we are eliminating in this step
787 %% if #2=1, then only draw grid (provides hold frame at start)
788 noindent%
789 begin{minipage}{MinipageScalelinewidth}%
790 centering%
791 begin{tikzpicture}[scale=Scale]%
792 BuildFrameInternals[#1]{#2};
793 end{tikzpicture}%
794 %
795 AddVerticalSpearationForStepByStep% Better spacing for Step by Step
796 end{minipage}%
797}%
798
799newcommand*{BuildFinalFrame}{%
800 noindent%
801 begin{minipage}{MinipageScalelinewidth}%
802 centering%
803 begin{tikzpicture}[scale=Scale]%
804 AddTitleNode;% Print Main title if AnimateSieve is defined
805 AddSubTitleNode{};
806 DrawGridWithNumbers;
807 foreach p in {2,...,MaxValue}{%
808 IfNumberAlreadyMarked[NonPrime]{p}{%
809 }{%
810 AddToListOfPrimes[fill=PrimeColor]{p};
811 ColorMultiplesOf{p};
812 }%
813 }%
814 pgfmathtruncatemacro{StartValue}{MaxValue+1}%
815 foreach p in {StartValue,...,MaxNumber}{%
816 IfNumberAlreadyMarked[NonPrime]{p}{%
817 %% This number has already been marked as non-prime
818 }{%
819 %% This is a prime. Since we are just printing out
820 %% the final results we don't distinguish between a
821 %% newly found prime and a prime found previously.
822 AddToListOfPrimes[fill=PrimeColor]{p};
823 }%
824 }%
825
826 ShowListOfPrimesNode;
827 end{tikzpicture}%
828 %
829 AddVerticalSpearationForStepByStep% Better spacing for Step by Step
830 end{minipage}%
831}
832
833
834
835begin{document}
836ifdefinedAnimateSieve
837 newcounter{CountK}
838 newcounter{CountP}
839 newcounter{CurrentMaxMultiplePlusOne}
840 %
841 begin{animateinline}{FrameRate}%
842 stepcounter{StepNumber}%
843 setcounter{CountK}{0}%
844 whiledo{arabic{CountK} < FramesToHoldAtStart}{%
845 BuildFrame[0]{1}% initial hold frame
846 newframe[FrameRate]%
847 stepcounter{CountK}%
848 }%
849 %
850 setcounter{CountK}{2}%
851 whiledo{numexprarabic{CountK}-1 < MaxValue}{%
852 IfNumberAlreadyMarked[NonPrime]{arabic{CountK}}{%
853 %% value{CountK} has already been marked as non-prime.
854 %% Hence, so so are its multiples, and we can skip it.
855 }{%
856%% Question 1: Should be able to replace three lines following with
857%% this. But then animation seems to skip the loop below
858% pgfmathsetcounter{CurrentMaxMultiplePlusOne}{1+(MaxNumber/arabic{CountK})}%
859 pgfmathtruncatemacro{MaxMultiple}{MaxNumber/arabic{CountK}}%
860 setcounter{CurrentMaxMultiplePlusOne}{MaxMultiple}%
861 stepcounter{CurrentMaxMultiplePlusOne}%
862 %
863 setcounter{CountP}{2}%
864 stepcounter{StepNumber}%
865%% Question 2: Ideally would prefer to use the following syntax
866%% but this does not even compile!!! But, an indentical
867%% syntax works in the above `whiledo`, where the value of
868%% MaxValue was also defined by pgfmathtruncatemacro
869% whiledo{numexprarabic{CountP}-1 < MaxMultiple}{%
870 whiledo{arabic{CountP} < arabic{CurrentMaxMultiplePlusOne}}{%
871 BuildFrame[theCountP]{theCountK}%
872 newframe[FrameRate]%
873 stepcounter{CountP}%
874 }%
875 }%
876 stepcounter{CountK}%
877 }%
878 % At end, add hold frames in case we are looping
879 %
880 % There needs to be enough of these so that each of the
881 % primes (those not colored in) get highlighted at each frame.
882 %
883 setcounter{CountK}{2}%
884 whiledo{numexprarabic{CountK}-1 < FramesToHoldAtEnd}{%
885 BuildFrame{MaxValue}%
886 newframe[FrameRate]%
887 stepcounter{CountK}%
888 }
889 end{animateinline}%
890elseifdefinedShowStepByStep
891 parbox{0.95linewidth}{centeringTitlenewline}%
892 bigskippar%
893 setcounter{StepNumber}{1}%
894 BuildFrame[0]{1}% Initial frame
895 hfill%
896 %
897 foreach k in {2,...,MaxValue}{%
898 IfNumberAlreadyMarked[NonPrime]{k}{%
899 % k has already been marked as non-prime.
900 % Hence, so so are its multiples, and we can skip it.
901 }{%
902 % This is a prime, so mark it as such and mark all the
903 % multiples up to MaxMultipleOfK as non-prime
904 stepcounter{StepNumber}%
905 pgfmathtruncatemacro{MaxMultipleOfK}{MaxNumber/k}%
906 BuildFrame[MaxMultipleOfK]{k}%
907 hfill%
908 }%
909 }%
910 %
911 stepcounter{StepNumber}%
912 BuildFinalFrame% Final Frame
913else% We only want to show the final frame
914 parbox{0.95linewidth}{centeringTitle}
915 setcounter{StepNumber}{0}
916 par
917 BuildFinalFrame%
918fi% ifdefinedShowStepByStep
919fi% ifdefinedAnimateSieve
920end{document}
921
922documentclass{article}
923usepackage[margin=1in]{geometry}
924pagestyle{empty}
925
926usepackage{tikz}
927usepackage{ifthen}
928
929newcommand{setxy}[1]{
930 pgfmathtruncatemacro{x}{Mod(#1,cols)}
931 pgfmathtruncatemacro{y}{#1 / cols}
932 pgfmathtruncatemacro{y}{cols - 1 - y}
933 pgfmathparse{2.5*(x+.5)}letxpgfmathresult
934 pgfmathparse{2.5*(y+.5)}letypgfmathresult
935}
936
937newcommand{polygon}[2]{
938 setxy{#1}
939 ifthenelse{#2>1}{ % Polygon must have at least 2 sides.
940 ifthenelse{#2<20}{ % Draw polygon if it has a small number of sides.
941 filldraw (x,y) +(90:1)
942 foreach i in {1,...,#2} {-- +(i/#2*360+90:1)} -- cycle;
943 }{ % Else approximate with circle.
944 filldraw (x,y) circle(1);
945 }
946 }{}
947}
948
949newcommand{numlabel}[1]{
950 setxy{n}
951 % Simulate a white outline around the black text.
952 foreach xs in {-.5,-.25,0,.25,.5} {
953 foreach ys in {-.5,-.25,0,.25,.5} {
954 node[fill=none, opacity=.25, text=black!10!white,
955 xshift=xs, yshift=ys] at (x,y) {tinyn}; }}
956 % Now draw the black text.
957 node[fill=none, text=black] at (x,y) {tinyn};
958}
959
960
961newcommand{sieve}[2]{
962 defcols{#1}
963 defrows{#2}
964 begin{tikzpicture}[scale=.5]
965 pgfmathtruncatemacro{nmax}{rows * cols - 1}
966 % Draw light-colored polygon outlines for all numbers.
967 begin{scope}[fill=white, draw=black!5!white, line width=4]
968 foreach n in {0,...,nmax} {polygon{n}{n}}
969 end{scope}
970 % Draw thin-dark-lined and slightly filled polygons at intervals.
971 begin{scope}[fill=black, fill opacity=.08,
972 draw=black, draw opacity=1,
973 line width=.5]
974 foreach n in {2,...,nmax} {
975 pgfmathparse{n+1}letmpgfmathresult
976 foreach i in {m,...,nmax} {
977 pgfmathparse{Mod(i,n)==0? 1:0}
978 ifnumpgfmathresult=1
979 polygon{i}{n}
980 fi
981 }
982 }
983 end{scope}
984 % Draw numeric labels.
985 begin{scope}[fill=none, draw=black]
986 foreach n in {0,...,nmax} {numlabel{n}}
987 end{scope}
988 end{tikzpicture}
989}
990
991begin{document}
992sieve{12}{16}
993end{document}
994
995documentclass{article}
996usepackage[margin=.25in]{geometry}
997pagestyle{empty}
998
999usepackage{tikz}
1000usepackage{ifthen}
1001
1002newcommand{setisprime}[1]{
1003 % Sets isprime based on #1.
1004 ifnum#1=1 gdefisprime{0} else gdefisprime{1} fi
1005 foreach sip in {2, 3,5,...,#1} {
1006 pgfmathparse{sip*sip>#1? 1:0}
1007 ifthenelse{pgfmathresult=1}{
1008 % Early-out if sip^2 > #1.
1009 breakforeach
1010 }{
1011 % Otherwise test if sip divides #1.
1012 pgfmathparse{Mod(#1,sip)==0? 1:0}
1013 ifthenelse{pgfmathresult=1}{
1014 gdefisprime{0}
1015 breakforeach
1016 }{}
1017 }
1018 }
1019}
1020
1021newcommand{setxy}[1]{
1022 % Sets x and y to loction of cell #1.
1023 pgfmathtruncatemacro{x}{Mod(#1-1,cols)}
1024 pgfmathtruncatemacro{y}{(#1-1) / cols}
1025 pgfmathtruncatemacro{y}{cols - 1 - y}
1026 pgfmathparse{2.5*(x+.5)}letxpgfmathresult
1027 pgfmathparse{2.5*(y+.5)}letypgfmathresult
1028}
1029
1030newcommand{numlabel}[2]{
1031 % Draws label #2 at cell #1.
1032 setxy{n}
1033 node[fill=none, text=black] at (x,y) {#2};
1034}
1035
1036newcommand{drawpolygon}[2]{
1037 % Draws polygon with #2 vertexes at cell #1.
1038 setxy{#1}
1039 ifthenelse{#2>1}{ % Polygon must have at least 2 sides.
1040 ifthenelse{#2<30}{ % Draw polygon if it has a small number of sides.
1041 filldraw (x,y) +(90:1)
1042 foreach drawi in {1,...,#2} {-- +(drawi/#2*360+90:1)} -- cycle;
1043 }{ % Else approximate with circle.
1044 filldraw (x,y) circle(1);
1045 }
1046 }{}
1047}
1048
1049newcommand{setpolygoncolor}[1]{
1050 % Sets color based on #1.
1051 gdefpolycolor{black}
1052 ifnum#1=2gdefpolycolor{black!50!white}fi
1053 ifnum#1=3gdefpolycolor{yellow!95!red}fi
1054 ifnum#1=5gdefpolycolor{yellow!0!red}fi
1055 ifnum#1=7gdefpolycolor{blue!75!green}fi
1056 ifnum#1=11gdefpolycolor{blue!70!red}fi
1057 ifnum#1=13gdefpolycolor{blue!40!red}fi
1058 ifnum#1=17gdefpolycolor{green!50!blue}fi
1059 ifnum#1=19gdefpolycolor{green!80!black}fi
1060 ifnum#1=23gdefpolycolor{green!50!red}fi
1061 ifnum#1=29gdefpolycolor{yellow!50!black}fi
1062 ifnum#1=31gdefpolycolor{orange!50!black}fi
1063 ifnum#1=37gdefpolycolor{red!50!black}fi
1064 ifnum#1=41gdefpolycolor{purple!50!black}fi
1065 ifnum#1=43gdefpolycolor{blue!50!black}fi
1066 ifnum#1=47gdefpolycolor{green!50!black}fi
1067 ifnum#1=53gdefpolycolor{white!50!black}fi
1068 ifnum#1=59gdefpolycolor{white!50!black}fi
1069 ifnum#1=61gdefpolycolor{white!50!black}fi
1070 ifnum#1=67gdefpolycolor{white!50!black}fi
1071}
1072
1073newcommand{sieve}[2]{
1074 defcols{#1}
1075 defrows{#2}
1076 begin{tikzpicture}[scale=.5]
1077 pgfmathtruncatemacro{nmax}{rows * cols}
1078
1079 foreach n in {1,...,nmax} {
1080 begin{scope}[fill=gray, fill opacity=.05,
1081 draw=gray, draw opacity=.10,
1082 line width=4]
1083 drawpolygon{n}{n}
1084 end{scope}
1085 setisprime{n}
1086 ifthenelse{isprime=1}{
1087 numlabel{n}{bfn}
1088 }{
1089 defstartintensity{.33}
1090 defincrintensity{.10}
1091 defintensity{startintensity}
1092
1093 defm{n}
1094 pgfmathtruncatemacro{i}{m / 2}
1095
1096 % Divide m by i until m is extinguished.
1097 % Increment i each time it does not divide into m.
1098 whiledo{m>1}{
1099 setisprime{i}
1100 pgfmathparse{Mod(m,i)==0? 1:0}
1101 ifthenelse{pgfmathresult=1andisprime=1}{
1102 setpolygoncolor{i}
1103 begin{scope}[fill=polycolor, fill opacity=intensity,
1104 draw=polycolor!85!black, draw opacity=intensity,
1105 line width=intensity*1.5]
1106 drawpolygon{n}{i}
1107 end{scope}
1108 pgfmathtruncatemacro{m}{m / i}
1109 pgfmathparse{intensity + incrintensity}letintensitypgfmathresult
1110 }{
1111 pgfmathtruncatemacro{i}{i - 1}
1112 defintensity{startintensity}
1113 }
1114 }
1115 begin{scope}[text=black, text opacity=.5]
1116 numlabel{n}{scriptsizen}
1117 end{scope}
1118 }
1119 }
1120
1121 end{tikzpicture}
1122}
1123
1124begin{document}
1125sieve{10}{10}
1126end{document}
1127
1128documentclass{article}
1129usepackage{tikz}
1130
1131directlua{dofile('sieve.lua')}
1132
1133tikzset{eratosthenes/.style={
1134 fill=eratosthenes #1 colour,
1135 eratosthenes #1 style/.try,
1136 },
1137 eratosthenes 0 style/.style={text=white},
1138}
1139colorlet{eratosthenes 0 colour}{white}
1140colorlet{eratosthenes 1 colour}{red}
1141colorlet{eratosthenes 2 colour}{orange}
1142colorlet{eratosthenes 3 colour}{yellow}
1143colorlet{eratosthenes 4 colour}{green}
1144colorlet{eratosthenes 5 colour}{blue}
1145colorlet{eratosthenes 6 colour}{purple}
1146
1147begin{document}
1148begin{tikzpicture}
1149foreach n in {2,...,100} {%
1150 node[
1151 draw,
1152 anchor=north west,
1153 minimum size=1cm,
1154 eratosthenes=directlua{tex.print(styles[n])}
1155 ] at ({Mod((n-1),10)},{-floor((n-1)/10)}) {n};
1156}
1157end{tikzpicture}
1158end{document}
1159
1160documentclass{article}
1161PassOptionsToPackage{dvipsnames,svgnames}{xcolor}
1162usepackage{tikz}
1163makeatletter
1164newcommandtestcolorednode[3]{%
1165 pgfutil@ifundefined{pgf@sh@ns@#1}{#2}{#3}}% is named node ?
1166makeatother
1167
1168begin{document}
1169begin{tikzpicture}[every node/.style={minimum size= 1cm,circle}]
1170 foreach nb/col in {2/blue,3/green,5/orange,7/red} {%
1171 pgfmathtruncatemacro{nbi}{nb}
1172 loop
1173 pgfmathtruncatemacro{nbi}{nbi+nb}
1174 testcolorednode{nbi}{%
1175 node[ball color=col!20](nbi) at ({Mod((nbi-1),10)},{-floor((nbi-1)/10)})
1176 {nbi};}{}
1177 pgfmathtruncatemacro{nbt}{nbi+nb}%
1178 ifnumnbt<101 repeat}% end foreach now we can complete the table with primes
1179foreach nb in {2,...,100}
1180 {testcolorednode{nb}{%
1181 node[ball color=Gold](nb) at ({Mod((nb-1),10)},{-floor((nb-1)/10)}) {nb};}{}}%
1182end{tikzpicture}
1183end{document}
1184
1185documentclass{article}
1186usepackage{tikz}
1187
1188begin{document}
1189 defmyscale{1}
1190begin{tikzpicture}[y=-1cm,scale=myscale]
1191
1192makeatletter
1193 foreach i/c in {2/purple,3/blue,5/yellow,7/red} {%
1194 pgfmathtruncatemacro{start}{i+1}
1195 foreach t in {start,...,100} {%
1196 pgfmathparse{mod(t,i)==0? 0 : 1}
1197 ifnum pgfmathresult=0
1198 pgfutil@ifundefined{pgf@sh@ns@t}{%
1199 pgfmathparse{mod(t,10)} letycpgfmathresult
1200 pgfmathparse{mod(t,10)==0? 1 : 0}
1201 ifnum pgfmathresult=1 pgfmathtruncatemacro{yc}{yc+10} fi
1202 pgfmathparse{(t-yc)/10)} letxcpgfmathresult
1203 node[minimum size= 1cm*myscale,fill=c!50,](t) at (yc-1,xc+1) {};}{}
1204 fi
1205}%
1206}
1207 foreach x in {0,...,9}
1208 foreach y in {1,...,10}
1209 {%
1210 pgfmathtruncatemacro{nb}{x*10+y}
1211 draw (x,y) +(-.5cm,-.5cm) rectangle ++(.5cm,.5cm);
1212 node[minimum size= 1cm*myscale] at (y-1,x+1) {nb} ;
1213}
1214end{tikzpicture}
1215end{document}
1216
1217node[minimum size= 1cm*myscale](t) at (yc-1,xc+1) {};
1218 draw[c,ultra thick](t.north west)--(t.south east)
1219 (t.north east)--(t.south west); }{}
1220
1221documentclass{article}
1222usepackage{tikz}
1223
1224begin{document}
1225
1226begin{tikzpicture}[y=-1cm]
1227 foreach x in {0,...,9}
1228 foreach y in {1,...,10}
1229 { draw (x,y) +(-.5cm,-.5cm) rectangle ++(.5cm,.5cm);
1230 pgfmathtruncatemacro{nb}{x*10+y}
1231 ifnumnb=1 defpgfmathresult{1}
1232 else
1233 node[minimum size= 1cm](last) at (y-1,x+1) {nb} ;
1234 pgfmathisprime{nb}
1235 fi
1236 ifnumpgfmathresult=0
1237 draw[red](last.north west)--(last.south east)
1238 (last.north east)--(last.south west);
1239 fi}
1240end{tikzpicture}
1241
1242end{document}
1243
1244tkzutil@ifundefined{pgfmath@function@isprime}{%
1245newififpgf@unknown
1246newififpgf@isaprime
1247pgfmathdeclarefunction{isprime}{1}{%
1248 begingroup%
1249pgf@unknowntrue
1250c@pgfmath@counta#1relax%
1251 ifcase#1relax
1252 % |#1| = 0
1253 pgf@isaprimefalsepgf@unknownfalse
1254 or
1255 % |#1| = 1
1256 pgf@isaprimefalsepgf@unknownfalse
1257 or
1258% |#1| = 2
1259 pgf@isaprimetruepgf@unknownfalse
1260 or
1261% |#1| = 3
1262 pgf@isaprimetruepgf@unknownfalse
1263 else
1264 % |#1| > 3
1265 ifodd#1relax
1266 else
1267 pgf@isaprimefalsepgf@unknownfalse
1268 fi
1269 fi
1270ifpgf@unknown
1271c@pgfmath@countd=thr@@ pgf@isaprimetrue
1272 loop
1273 c@pgfmath@countb=c@pgfmath@counta
1274 dividec@pgfmath@countb byc@pgfmath@countd
1275 ifnumc@pgfmath@countb>c@pgfmath@countd pgf@unknowntrue
1276 elsepgf@unknownfalsefi
1277 multiplyc@pgfmath@countb byc@pgfmath@countd
1278 ifnumc@pgfmath@countb=c@pgfmath@counta
1279 globalpgf@isaprimefalsepgf@unknownfalsefi
1280 ifpgf@unknownadvancec@pgfmath@countd by 2relax%
1281 repeat
1282fi
1283ifpgf@isaprime
1284 defpgfmathresult{1}%
1285else
1286 defpgfmathresult{0}%
1287fi
1288 pgfmath@smuggleonepgfmathresult%
1289endgroup
1290}}{}
1291
1292documentclass{article}
1293usepackage{tikz}
1294usetikzlibrary{backgrounds}
1295begin{document}
1296begin{tikzpicture}
1297draw foreach p in {0.5,1.5,...,10.5} {(p,0.5)--(p,10.5) (0.5,p)--(10.5,p)};
1298foreach[count=ccolno] x in {0,10,...,90}{
1299 foreach[count=rrowno] y in {1,...,10}
1300 {
1301 pgfmathtruncatemacro{value}{x+y}
1302 node[minimum size=0.8cm] (nvalue) at (rrowno,ccolno) {value};
1303 }
1304}
1305foreach [evaluate=x as shade using x*10] x in {2,...,10}{
1306 foreach y in {4,6,8,9,...,100}
1307 {
1308 pgfmathparse{Mod(y,x)==0? 1 : 0}
1309 ifnum pgfmathresult=1
1310 begin{scope}[on background layer]
1311 node[fill=yellow!shade!white,opacity=0.2,inner sep=5mm] at (ny) {};
1312 end{scope}
1313 draw[red] (ny.north east)--(ny.south west) (ny.north west)--(ny.south east) ;
1314 fi
1315 }
1316}
1317node[fill=white,minimum size=0.9cm] at (n1) {}; % 1 is not included
1318end{tikzpicture}
1319end{document}
1320
1321documentclass{standalone}
1322usepackage{tikz}
1323tikzset{
1324 number/.style={circle,inner sep=1pt},
1325 prime number/.style={number,fill=lime},
1326 non prime number/.style={number,fill=white,draw=red!50},
1327}
1328begin{document}
1329begin{tikzpicture}[font=bfseriestiny]
1330 coordinate (last);
1331 foreach nb in {0,...,100}{
1332 pgfmathsetmacro{r}{sqrt(nb)*.5}
1333 pgfmathsetmacro{a}{sqrt(nb)*360}
1334 coordinate(new) at (a:r);
1335 draw[dotted] (last) to[bend right={6/sqrt(nb)}] (new);
1336 pgfmathisprime{nb}
1337 ifnumpgfmathresult=0
1338 node[non prime number](last) at (a:r) {nb};
1339 else
1340 node[prime number](last) at (a:r) {nb};
1341 fi
1342 }
1343end{tikzpicture}
1344end{document}