Question: This spiral has several interesting features. The southeast diagonal has several prime numbers (3, 13, 31, 57, and 91) along it. The southwest diagonal has
This spiral has several interesting features. The southeast diagonal has several prime numbers (3, 13, 31, 57, and 91) along it. The southwest diagonal has a weaker concentration of prime numbers (5, 17, 37) along it.
To construct the spiral we start with 1 at the center, with 2 to the right, and 3 below it, 4 to the left, and so on. A part of the problem for this assignment is to figure out the rule to fill the spiral for an arbirary size. Once you have that rule you can complete the rest of the assignment.
Input: You will read your input data from a file called spiral.in. The format of the file will be as follows:
11 1 42 110 91
The first line will be the dimension of the spiral. It will always be odd and greater than 1 and less than 100. This will be followed by an arbitrary number of lines. There will be a single number on each line. These numbers will be numbers inside the spiral. Some of these numbers will be interior numbers, others will be numbers on the edge, and yet others will be numbers at the corners of the spirals. Assume that the input file that we will be testing your program will be valid.
Output: For each of the numbers inside the spiral, your output will be the sum of all the numbers adjacent to this number, but not including this number. For the above input file you will output on the console:
44 382 477 239
We get 44 by adding the numbers adjacent to 1 (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9). Similarly we get 239 by adding the numbers adjacent to 91 (57 + 90 + 92).
You will read your input from stdin like so:
Mac: python3 Spiral.pyWe will use our own input file to test your program. You must read the input in the format described above. Once you read the first line from the input file you will create a 2-D list with the spiral of numbers. Then from the 2-D list you will obtain the sum of adjacent numbers for a given number in the spiral and print it. The number of lines of input will be arbitrary and greater than 1.
112 114 1116 ||118 1119 |120 1121 | 113 | 74 |115 || 6 45 |117 | 78 179 || 1 || 2 |80 |49 43 |77 |46 23 47 148 |50 83 73 172 171 |70 169 |75 | 144 | 21 |20 |26 184 |42 41 |40 22 17 24 19 9 | |10 |111 |110 109 |108 107 106 | 105 | 104 103 | 102 I101 18 |51 2 8 85 19 16 || 11 27 28 9 30 || 26 187 168 || 14 |15 39 | ||16 | 12 13 ||67 38 || 8 16 37 || 8 |17 |36 |63 198 135 34 3 3 |14 || 3 || 0 193 137 31 || 4 || 5 || 6 || 7 || 2 189 166 164 162 61 58 190 || 9 194 | 100 ||99 97 196 193 191 112 114 1116 ||118 1119 |120 1121 | 113 | 74 |115 || 6 45 |117 | 78 179 || 1 || 2 |80 |49 43 |77 |46 23 47 148 |50 83 73 172 171 |70 169 |75 | 144 | 21 |20 |26 184 |42 41 |40 22 17 24 19 9 | |10 |111 |110 109 |108 107 106 | 105 | 104 103 | 102 I101 18 |51 2 8 85 19 16 || 11 27 28 9 30 || 26 187 168 || 14 |15 39 | ||16 | 12 13 ||67 38 || 8 16 37 || 8 |17 |36 |63 198 135 34 3 3 |14 || 3 || 0 193 137 31 || 4 || 5 || 6 || 7 || 2 189 166 164 162 61 58 190 || 9 194 | 100 ||99 97 196 193 191
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
