Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions project/euler/digit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package euler

// CalculateDigitNumber 桁数を計算する
func CalculateDigitNumber(n int) int {
if n == 0 {
return 1
}
if n < 0 {
n *= -1
}
m := 0
for i := 1; n/i > 0; i *= 10 {
m++
}
return m
}

// CirculateDigit 末尾の桁の數字を先頭に移動する
func CirculateDigit(n int) int {
minus := false
if n < 0 {
minus = true
n *= -1
}

m := 1
for n/(10*m) > 0 {
m *= 10
}
h := n / 10
t := n % 10

l := t*m + h
if minus {
return -1 * l
}
return l
}
58 changes: 58 additions & 0 deletions project/euler/digit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package euler

import "testing"

func TestCalculateDigitNumber(t *testing.T) {
cases := []struct {
Input int
Expected int
}{
{-10, 2},
{-1, 1},
{0, 1},
{1, 1},
{3, 1},
{10, 2},
{41, 2},
{100, 3},
{213, 3},
}
for _, tc := range cases {
if actual := CalculateDigitNumber(tc.Input); actual != tc.Expected {
t.Errorf(
"Input:%v\nExpected:%v\nActual:%v",
tc.Input,
tc.Expected,
actual,
)
}
}
}

func TestCirculateDigit(t *testing.T) {
cases := []struct {
Input int
Expected int
}{
// 負數
{-1, -1}, {-10, -1}, {-12, -21},
// 0
{0, 0},
// 正數
{1, 1}, {2, 2},
{10, 1}, {12, 21},
{100, 10}, {102, 210}, {120, 12}, {123, 312},
{1000, 100}, {1002, 2100}, {1020, 102}, {1200, 120},
{1023, 3102}, {1203, 3120}, {1230, 123}, {1234, 4123},
}
for _, tc := range cases {
if actual := CirculateDigit(tc.Input); actual != tc.Expected {
t.Errorf(
"Input:%v\nExpected:%v\nActual:%v",
tc.Input,
tc.Expected,
actual,
)
}
}
}
14 changes: 14 additions & 0 deletions project/euler/pe0026-0050.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,17 @@ func PE0034() int {
}
return sum
}

// PE0035 1,000,000以下の巡回素数の個数を計算
//
// https://projecteuler.net/problem=35
func PE0035(n int) int {
// n以下の素数列
ps := []int{}
pg := NewPrimeGenerator()
for p := pg.Next(); p <= n; p = pg.Next() {
ps = append(ps, p)
}

return 0
}
22 changes: 22 additions & 0 deletions project/euler/pe0026-0050_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,26 @@ func TestPE0034(t *testing.T) {

// }}}

// PE0035 {{{
func TestPE0035(t *testing.T) {
cases := []struct {
Input int
Expected int
}{
{1000000, 0},
}
for _, tc := range cases {
if actual := PE0035(tc.Input); actual != tc.Expected {
t.Errorf(
"Input:%v\nExpected:%v\nActual:%v",
tc.Input,
tc.Expected,
actual,
)
}
}
}

// }}}

// vim:set foldmethod=marker: