实现等分和和完美数算法

时间:2018-11-23 15:59:44

标签: elixir

根据Nicomachus(60-120 CE)对自然数的分类方案,确定数字是完善的,丰富的还是不足的。

希腊数学家尼科马修斯(Nicomachus)设计了一种自然数分类方案,根据等分和确定每个自然数分别属于完美,丰富或不足的类别。等分和定义为不包括数字本身的数字因子之和。例如,等分和15为(1 + 3 + 5)= 9

Perfect: aliquot sum = number
    6 is a perfect number because (1 + 2 + 3) = 6
    28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
Abundant: aliquot sum > number
    12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
    24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
Deficient: aliquot sum < number
    8 is a deficient number because (1 + 2 + 4) = 7
    Prime numbers are deficient

1 个答案:

答案 0 :(得分:1)

这是一个入门的实现,虽然它仅适用于正整数,并且可以使用一些改进来提高性能:

runtime: php55
api_version: 1
threadsafe: true

env_variables:

handlers:
- url: /(.*\.(appcache|manifest))
  mime_type: text/cache-manifest
  static_files: static/\1
  upload: static/(.*\.(appcache|manifest))

- url: /dialpad_research/(.*\.(appcache|manifest))
  mime_type: text/cache-manifest
  static_files: dialpad_research/static/\1
  upload: dialpad_research/static/(.*\.(appcache|manifest))

- url: /(.*\.atom)
  mime_type: application/atom+xml
  static_files: static/\1
  upload: static/(.*\.atom)

- url: /(.*\.crx)
  mime_type: application/x-chrome-extension
  static_files: static/\1
  upload: static/(.*\.crx)

- url: /(.*\.css)
  mime_type: text/css
  static_files: static/\1
  upload: static/(.*\.css)

- url: /(.*\.eot)
  mime_type: application/vnd.ms-fontobject
  static_files: static/\1
  upload: static/(.*\.eot)

- url: /(.*\.htc)
  mime_type: text/x-component
  static_files: static/\1
  upload: static/(.*\.htc)

- url: /(.*\.html)
  mime_type: text/html
  static_files: static/\1
  upload: static/(.*\.html)

- url: /(.*\.ico)
  mime_type: image/x-icon
  static_files: static/\1
  upload: static/(.*\.ico)

- url: /(.*\.js)
  mime_type: text/javascript
  static_files: static/\1
  upload: static/(.*\.js)

- url: /(.*\.json)
  mime_type: application/json
  static_files: static/\1
  upload: static/(.*\.json)

- url: /(.*\.otf)
  mime_type: font/opentype
  static_files: static/\1
  upload: static/(.*\.otf)

- url: /(.*\.rss)
  mime_type: application/rss+xml
  static_files: static/\1
  upload: static/(.*\.rss)

- url: /(.*\.safariextz)
  mime_type: application/octet-stream
  static_files: static/\1
  upload: static/(.*\.safariextz)

- url: /(.*\.(svg|svgz))
  mime_type: images/svg+xml
  static_files: static/\1
  upload: static/(.*\.(svg|svgz))

- url: /(.*\.swf)
  mime_type: application/x-shockwave-flash
  static_files: static/\1
  upload: static/(.*\.swf)

- url: /(.*\.ttf)
  mime_type: font/truetype
  static_files: static/\1
  upload: static/(.*\.ttf)

- url: /(.*\.txt)
  mime_type: text/plain
  static_files: static/\1
  upload: static/(.*\.txt)

- url: /(.*\.unity3d)
  mime_type: application/vnd.unity
  static_files: static/\1
  upload: static/(.*\.unity3d)

- url: /(.*\.webm)
  mime_type: video/webm
  static_files: static/\1
  upload: static/(.*\.webm)

- url: /(.*\.webp)
  mime_type: image/webp
  static_files: static/\1
  upload: static/(.*\.webp)

- url: /(.*\.woff)
  mime_type: application/x-font-woff
  static_files: static/\1
  upload: static/(.*\.woff)

- url: /(.*\.xml)
  mime_type: application/xml
  static_files: static/\1
  upload: static/(.*\.xml)

- url: /(.*\.xpi)
  mime_type: application/x-xpinstall
  static_files: static/\1
  upload: static/(.*\.xpi)

# audio files
- url: /(.*\.(mid|midi|mp3|wav))
  static_files: static/\1
  upload: static/(.*\.(mid|midi|mp3|wav))

# windows files
- url: /(.*\.(doc|exe|ppt|rtf|xls))
  static_files: static/\1
  upload: static/(.*\.(doc|exe|ppt|rtf|xls))

# compressed files
- url: /(.*\.(bz2|gz|rar|tar|tgz|zip))
  static_files: static/\1
  upload: static/(.*\.(bz2|gz|rar|tar|tgz|zip))

# index files
- url: /(.*)/
  static_files: static/\1/index.html
  upload: static/(.*)/index.html

- url: /dialpad_research/api/(.+\.php)$
  script: dialpad_research/api/\1

- url: /vendor/(.+\.php)$
  script: vendor/\1

# site root
- url: /
  static_files: static/index.html
  upload: static/index.html


- url: /static
  static_dir: static


- url: /dialpad_research/static
  static_dir: dialpad_research/static

# dialpad root
- url: /dialpad_research.*
  static_files: dialpad_research/static/index.html
  upload: dialpad_research/static/index.html

# portfolio redirect
- url: /portfolio/.*
  static_files: static/red.html
  upload: static/red.html

# G2 redirect
- url: /g2.*
  static_files: static/red.html
  upload: static/red.html

# Blog redirect
- url: /blog.*
  static_files: static/red.html
  upload: static/red.html

# SSDESIGN redirect
- url: /ssdesign.*
  static_files: static/red.html
  upload: static/red.html

# Colours redirect
- url: /colours.*
  static_files: static/red.html
  upload: static/red.html

按预期工作:

defmodule PerfectNumber do
  def check(n) do
    sum = aliquot_sum(n)

    cond do
      sum == n -> :perfect
      sum <  n -> :deficient
      sum >  n -> :abundant
    end
  end

  def aliquot_sum(n) do
    Enum.sum(factors(n))
  end

  def factors(1), do: [1]
  def factors(n) do
    for i <- 1..div(n,2), rem(n,i) == 0, do: i
  end
end