Example #1
0
func (gen *Generator) LenOfNextCirInGroup(start, target int) (len uint64, sum uint64) {
	if start == target {
		sum, circled, ok := gen.groups[start].nextValue()
		if !circled || !ok {
			gen.groups[start].Undo()
			fmt.Printf("Group %d ran out of accessible circled numbers\n", start)
			return 0, 0
		}
		return 1, sum
	}
	newestCircled, _, _ := gen.groups[start].nextValue()
	sum += newestCircled

	nonCircledCount, circledCount := uint64(1), floorSqrt.SumFSqRange(newestCircled, 1)
	//TODO: handle the ok
	newestCircled, _, _ = gen.groups[start+1].peek()
	gen.groups[start+1].skipCircled(circledCount)
	sum = moduloMath.Sum(1e9, sum, moduloMath.SumRange(1e9, newestCircled, circledCount))
	for ii := start + 2; ii <= target; ii++ {
		nonCircledCount += circledCount
		circledCount += floorSqrt.SumFSqRange(newestCircled, circledCount)
		//TODO: handle the ok
		newestCircled, _, _ = gen.groups[ii].peek()
		gen.groups[ii].skipCircled(circledCount)
		sum = moduloMath.Sum(1e9, sum, moduloMath.SumRange(1e9, newestCircled, circledCount))
	}
	len = nonCircledCount + circledCount
	return
}
Example #2
0
func sequenceSum(nn uint64) (sum uint64) {
	gen := sequenceGenerator.NewGenerator()
	//get the target group and length up to that point
	targetGroup, len := sequenceGenerator.LastGroup(nn)
	nRemain := nn - len
	fmt.Printf("the %dth value of the sequence is the %dth value of group %d\n", nn, nRemain, targetGroup)
	sum = sequenceGenerator.SumFirstNGroups(targetGroup)
	for ii := 1; ii < targetGroup; ii++ {
		numTests := 0
		for gen.IsNextCircled(ii) {
			numTests++
			len, partialSum := gen.LenOfNextCirInGroup(ii, targetGroup)
			if len > nRemain {
				//fmt.Printf("next value from group %d comes after the target\n", ii)
				gen.UndoUpTo(targetGroup)
				break
			} else {
				nRemain -= len
				//fmt.Printf("next value from group %d comes before the target with %d values accounted for and %d remaining\n", ii, len, nRemain)
				sum = moduloMath.Sum(1e9, sum, partialSum)
				if nRemain == 0 {
					return
				}
			}
		}
		fmt.Printf("moving onto group %d after %d tests\n", ii+1, numTests)
	}
	finalSumStart := gen.NextCircled(targetGroup)
	finalSum := moduloMath.SumRange(1e9, finalSumStart, nRemain)
	fmt.Printf("final group sum over %d sequential values starting at %d\n", nRemain, finalSumStart)
	sum = moduloMath.Sum(1e9, sum, finalSum)
	return
}
Example #3
0
func SumFirstNGroups(nn int) (sum uint64) {
	sum = 1
	for ii := 0; ii < nn; ii++ {
		sum = moduloMath.Sum(1e9, sum, moduloMath.SumRange(1e9, 1, groupLengths[ii]))
	}
	return
}