Problem 28 "Number spiral diagonals"
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
問 28 「螺旋状に並んだ数の対角線」
1から初めて右方向に進み時計回りに数字を増やしていき, 5×5の螺旋が以下のように生成される:
両対角線上の数字の合計は101であることが確かめられる.
1001×1001の螺旋を同じ方法で生成したとき, 対角線上の数字の和はいくつか?
fn main() { let width = 1001u64; assert!(width % 2 == 1); let n = width / 2; let sum = 16 * n * (n + 1) * (2 * n + 1) / 6 + 4 * n * (1 + n) / 2 + 4 * n + 1; println!("{}", sum); assert_eq!(sum, 669171001); }