网易雷火,第4题简约数ac
package main import ( "fmt" ) /* f(2) = 2 - 1 f(3) = 3 - 1 f(5) = 5 - 1 ..... f(2 * 3) = f(2) * f(3) f(2 * 3 * 5) = f(2) * f(3) * f(5) ..... f(2 * 3) 到 f(2 * 3 * 5)区间内的最小值都为 f(2 * 3) */ func main() { var n int fmt.Scanln(&n) if n == 1 { fmt.Printf("%.6f\n", float64(1)) return } nums := make([]int, 0) for num := 2; num < 30; num++ { nums = append(nums, num) } p(nums) cur := 1 cnt := 1 for _, num := range primes { if cur * num > n { break } cur *= num cnt *= num-1 } fmt.Printf("%.6f\n", float64(cnt) / float64(cur)) } // 筛法求素数 var primes []int func p(nums []int) { if len(nums) == 0 { return } prime := nums[0] primes = append(primes, prime) left := make([]int, 0) for i := 1; i < len(nums); i++ { if nums[i] % prime != 0 { left = append(left, nums[i]) } } p(left) }
#笔试题目##网易雷火#