Skip to content

Latest commit

ย 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

README.md

Problem 21

Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a โ‰  b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Korean: http://euler.synap.co.kr/prob_detail.php?id=21
English: https://projecteuler.net/problem=21

System Requirement

Test - bash

Change directory git root: /Problem21 and compile

dotnet run

Run

/bin/Debug/netcoreapp3.1/Problem21.exe

Test - Visaul Studio Code

  • Open folder "Problem21" by Visual Studio Code
  • Check out settings: launch.json and tasks.json
  • Press Ctrl + Shift + B to Build
  • Press F5 to debug start

Divisors

  • ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋„ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์•Œ๊ฒŒ ๋œ ์ง€์‹์ด๊ธฐ๋„ ํ•˜๊ณ , ํ”„๋กœ์ ํŠธ ์˜ค์ผ๋Ÿฌ๋ฅผ ๊ด€ํ†ตํ•˜๋Š” ์ค‘์š”ํ•œ ์ˆ˜ํ•™์  ์ง€์‹์ด๊ธฐ๋„ ํ•˜๋‹ค.

Ready sum of divisors function

  • ๋ฌธ์ œ์—์„œ 220, 284์˜ ์•ฝ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๊ณ  ๊ทธ ํ•ฉ์„ ๋น„๊ต๋ฅผ ํ–ˆ์œผ๋ฏ€๋กœ
  • SumOfDivisor(n)์„ ์ค€๋น„ํ•˜๋Š”๋ฐ ์ด๊ฑด SumOfDivisor(220) == SumOfDivior(284)๊ฐ€ ๊ฐ™๋‹ค๋Š” ๊ฑธ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ค€๋น„ ํ•œ๋‹ค.
  • ์•ฝ์ˆ˜์˜ ํŠน์„ฑ์ƒ (๊ทธ๋ฆฌ๊ณ  ์†Œ์ˆ˜์˜ ํŠน์„ฑ์„ ํฌํ•จํ•˜์—ฌ) ์ตœ์†Œ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ ์ด์ƒ์˜ for๋ฌธ์„ ๋Œ๋ฆด ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ์ฆ‰, 220์˜ ๊ฒฝ์šฐ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ ๊ฐ€์žฅ ํฐ ์•ฝ์ˆ˜๋Š” 110์ด๊ณ  ์ด๋Š” ์†Œ์ˆ˜ 2๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š” ๋ชซ์ผ ๋•Œ 111 ~ 220 ๊นŒ์ง€์˜ ์ˆ˜ ์ค‘์— ์•ฝ์ˆ˜๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ์ฐพ์œผ๋ ค๊ณ  for loop์„ ๋Œ๋ฆด ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๋œป์ด๊ธฐ๋„ ํ•˜๋‹ค.
  • ์ด ํ•จ์ˆ˜๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ตœ์ ํ™”๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

Solve

  • 10000 ๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฆฌ๊ณ  ํ•ด๋‹น ์ •์ˆ˜์— ๋”ฐ๋ฅธ ์•ฝ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” SumOfDivisor(i)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ sum1์— ๋‹ด๋Š”๋‹ค.
  • ๋‹ค์‹œ SumOfDivisor(sum1)์„ ํ˜ธ์ถœํ•ด ๋ณด๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ sum2์— ๋‹ด๋Š”๋‹ค.
  • sum2 == i ์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค.
  • sum2 == i ๋ผ๋ฉด i + sum2๋ฅผ ํ•˜๊ณ  AmicableSum ๋ณ€์ˆ˜์— ๊ณ„์† ๋ˆ„์ ํ•œ๋‹ค.
  • ๋ฌธ์ œ์— d(a) = b and d(b) = a, where a โ‰  b ์กฐ๊ฑด์ด ์žˆ์œผ๋ฏ€๋กœ sum1 != sum2 ์กฐ๊ฑด์„ ๊ฑธ์–ด์ค€๋‹ค.
    • ์ฐธ๊ณ ๋กœ ์ด ์ˆซ์ž๋Š” 10000๊นŒ์ง€ for๋ฌธ ๋Œ๋ฆฌ๋ฉด 4๊ฐœ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ 6, 28, 496, 8628์ด ๋‚˜์˜จ๋‹ค.
    • ์ด ์ˆซ์ž๋Š” ์ œ์™ธํ•˜๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ์ตœ์ข… if๋ฌธ์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™๊ฒŒ ๋œ๋‹ค.
if (sum2 == i && sum1 != sum2)
  • ๋งˆ์ง€๋ง‰์œผ๋กœ AmicableSum ๊ฐ’์„ 2๋กœ ๋‚˜๋ˆ„์–ด ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด์œ ๋Š” 220์ผ ๋•Œ 220+280์„ ํ•˜๊ณ  280์ผ ๋•Œ๋„ 280+220์ด ๋˜๋ฉฐ ์ดํ›„ amicalble number๊ฐ€ ๋‘ ์Œ์œผ๋กœ ์ƒ์„ฑ๋˜๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ํ•ฉ์œผ๋กœ ์–ป์–ด๋‚ธ๋‹ค.