7 kyu
Simple Fun #16: Apple Boxes
364 of 1,039myjinxin2015
Description:
Task
There're k
square apple boxes full of apples. If a box is of size m
, then it contains m × m apples. You noticed two interesting properties about the boxes:
The smallest box is of size 1,
the next one is of size 2,...,
all the way up to size k.
Boxes that have an odd size contain only yellow apples.
Boxes that have an even size contain only red apples.
Your task is to calculate the difference between the number of red apples and the number of yellow apples.
Example
For k = 5, the output should be -15
There are 1 + 3 × 3 + 5 × 5 = 35 yellow apples and 2 × 2 + 4 × 4 = 20 red apples, thus the answer is 20 - 35 = -15.
Input/Output
[input]
integerk
A positive integer.
Constraints: 1 ≤ k ≤ 40
[output]
an integerThe difference between the two types of apples.
Puzzles
Similar Kata:
Stats:
Created | Jan 22, 2017 |
Published | Jan 22, 2017 |
Warriors Trained | 1475 |
Total Skips | 39 |
Total Code Submissions | 1960 |
Total Times Completed | 1039 |
JavaScript Completions | 364 |
C# Completions | 110 |
Python Completions | 429 |
PHP Completions | 164 |
Ruby Completions | 41 |
Prolog Completions | 6 |
Total Stars | 7 |
% of votes with a positive feedback rating | 93% of 195 |
Total "Very Satisfied" Votes | 170 |
Total "Somewhat Satisfied" Votes | 23 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 8 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 7 kyu |
Lowest Assessed Rank | 7 kyu |