Created
November 20, 2016 02:18
-
-
Save faultier/14693c16f8534dc96a78971683bb9e98 to your computer and use it in GitHub Desktop.
身内の結婚式で包むご祝儀候補算出
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func main() { | |
// 身内なので多めに包むとして5万円以上にする | |
// あんまり多くするとお財布的にも中袋に書く金額的にも辛いので多くても10万円程度にする | |
// 硬貨がジャラジャラすると重いしうるさいしいいことないので、お札に1円玉1枚の組み合わせにする | |
for i := 50000; i < 110000; i += 1000 { | |
x := i + 1 | |
// 9万円代は苦ってつくから避けたい | |
if x >= 90000 && x < 100000 { | |
continue | |
} | |
// 金額を素数にしたい | |
if !IsPrime(x) { | |
continue | |
} | |
// 紙幣+硬貨の枚数も素数にしたい | |
if !IsPrime(Count(x)) { | |
continue | |
} | |
fmt.Println(x, Count(x)) | |
} | |
} | |
// IsPrime 素数判定 | |
func IsPrime(x int) bool { | |
if x == 1 || x == 2 { | |
return true | |
} | |
if x%2 == 0 { | |
return false | |
} | |
rx := math.Sqrt(float64(x)) | |
for n := 2; float64(n) <= rx; n++ { | |
if x%n == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
// Count 必要な紙幣+硬貨の枚数 | |
func Count(x int) int { | |
c := 0 | |
r := x | |
// 端数は1円という前提なので紙幣の数だけ数える | |
// 2000円札は用意するのが面倒な割に貰っても扱いに困るのでないものとする | |
for _, n := range []int{10000, 5000, 1000} { | |
c = c + r/n | |
r = r % n | |
} | |
return c + r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment