func (me *StatisticalAccumulator) PutFrac64(a, b int64) {

	xx := new(big.Rat)
	xx.SetFrac64(a, b)

	me.PutRat(xx)
}
Exemplo n.º 2
0
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd', 'W', 'M', and 'Y' suffixes.
func ParseTimeDuration(value string) (int64, error) {
	var constant time.Duration

	prefixSize := 1

	switch value[len(value)-1] {
	case 'u':
		constant = time.Microsecond
	case 's':
		constant = time.Second
	case 'm':
		constant = time.Minute
	case 'h':
		constant = time.Hour
	case 'd':
		constant = 24 * time.Hour
	case 'w', 'W':
		constant = Week
	case 'M':
		constant = Month
	case 'y', 'Y':
		constant = Year
	default:
		prefixSize = 0
	}

	if value[len(value)-2:] == "ms" {
		constant = time.Millisecond
		prefixSize = 2
	}

	t := big.Rat{}
	timeString := value
	if prefixSize > 0 {
		timeString = value[:len(value)-prefixSize]
	}

	_, err := fmt.Sscan(timeString, &t)
	if err != nil {
		return 0, err
	}

	if prefixSize > 0 {
		c := big.Rat{}
		c.SetFrac64(int64(constant), 1)
		t.Mul(&t, &c)
	}

	if t.IsInt() {
		return t.Num().Int64(), nil
	}
	f, _ := t.Float64()
	return int64(f), nil
}
Exemplo n.º 3
0
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd' and 'w' suffixes.
func ParseTimeDuration(value string) (int64, error) {
	var constant time.Duration
	hasPrefix := true

	switch value[len(value)-1] {
	case 'u':
		constant = time.Microsecond
	case 's':
		constant = time.Second
	case 'm':
		constant = time.Minute
	case 'h':
		constant = time.Hour
	case 'd':
		constant = 24 * time.Hour
	case 'w':
		constant = 7 * 24 * time.Hour
	case 'y':
		constant = 365 * 24 * time.Hour
	default:
		hasPrefix = false
	}

	t := big.Rat{}
	timeString := value
	if hasPrefix {
		timeString = value[:len(value)-1]
	}

	_, err := fmt.Sscan(timeString, &t)
	if err != nil {
		return 0, err
	}

	if hasPrefix {
		c := big.Rat{}
		c.SetFrac64(int64(constant), 1)
		t.Mul(&t, &c)
	}
	if t.IsInt() {
		return t.Num().Int64(), nil
	}
	f, _ := t.Float64()
	return int64(f), nil
}
func main() {
	var recip big.Rat
	max := int64(1 << 19)
	for candidate := int64(2); candidate < max; candidate++ {
		sum := big.NewRat(1, candidate)
		max2 := int64(math.Sqrt(float64(candidate)))
		for factor := int64(2); factor <= max2; factor++ {
			if candidate%factor == 0 {
				sum.Add(sum, recip.SetFrac64(1, factor))
				if f2 := candidate / factor; f2 != factor {
					sum.Add(sum, recip.SetFrac64(1, f2))
				}
			}
		}
		if sum.Denom().Int64() == 1 {
			perfectstring := ""
			if sum.Num().Int64() == 1 {
				perfectstring = "perfect!"
			}
			fmt.Printf("Sum of recipr. factors of %d = %d exactly %s\n",
				candidate, sum.Num().Int64(), perfectstring)
		}
	}
}