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] integer k

    A positive integer.

    Constraints: 1 ≤ k ≤ 40

  • [output] an integer

    The difference between the two types of apples.

Puzzles

Stats:

CreatedJan 22, 2017
PublishedJan 22, 2017
Warriors Trained1475
Total Skips39
Total Code Submissions1960
Total Times Completed1039
JavaScript Completions364
C# Completions110
Python Completions429
PHP Completions164
Ruby Completions41
Prolog Completions6
Total Stars7
% of votes with a positive feedback rating93% of 195
Total "Very Satisfied" Votes170
Total "Somewhat Satisfied" Votes23
Total "Not Satisfied" Votes2
Total Rank Assessments8
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • JohanWiltink Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad