Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Q3

Recommended Posts

数字13195的素数元素是5,7,13和29

那么,数字600851475143 的最大素数元素是?
不支持并发的Go语言基础版本

package main

import (
	"fmt"
	"math"
	"sort"
)

func MinInt64ElOfSlice(v []int64) int64 {

	sort.Slice(v, func(i, j int) bool { return v[i] < v[j] })
	return v[0]
}

func MaxInt64ElOfSlice(v []int64) int64 {

	sort.Slice(v, func(i, j int) bool { return v[i] > v[j] })
	return v[0]
}

func getPrimeFactors(intV int64) []int64 {

	var sliceInstance []int64
	sliceInstance = make([]int64, 1)

	var i int64 = 3
	for intV%2 == 0 {
		intV = int64(intV / 2)
	}
	for ; i <= int64(math.Floor(math.Sqrt(float64(intV)))); i = i + 2 {

		for intV%i == 0 {
			sliceInstance = append(sliceInstance, i)
			intV = int64(intV / i)
		}

	}
	if intV > 2 {

		sliceInstance = append(sliceInstance, intV)
	}

	return sliceInstance[1:]
}
func main() {

	var intV int64 = 600851475143

	var sliceInstance []int64

	sliceInstance = getPrimeFactors(intV)

	fmt.Println(sliceInstance)
	fmt.Println(MaxInt64ElOfSlice(sliceInstance))

}

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×