Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #6921

Problem 512 Euler

Newsgroups comp.programming.threads
From Branimir Maksimovic <branimir.maksimovic@icloud.com>
Subject Problem 512 Euler
Message-ID <oan6J.5877$2m1.3477@fx26.iad> (permalink)
Organization usenet-news.net
Date 2021-10-03 19:05 +0000

Show all headers | View raw


import Foundation

@main
struct Main{
    static var factor : Int = 8;
    actor A{
        static func phi(_ nn : Int)->Int {
            var n = nn
            var m = n;
            if n % 2 == 0 {
                m /= 2;
                while n % 2 == 0 {
                    n /= 2;
                }
            }
            var i = 3;
            while (i*i <= n) {
                if n % i == 0 {
                    m /= i;
                    m *= (i-1);
                    while n % i == 0 {
                        n /= i
                    }
                }
                i += 2
            }
            if n > 1 {
                m /= n;
                m *= (n-1)
            }
            return m
        }
        static func f(_ n:Int)->Int {
            if n & 1 == 0 { return 0 }

            return phi(n)
        }
        func g(_ n : Int)async->Int{
            var sum = 0
            await withTaskGroup(of: Int.self) { group in
                for j in 0..<factor {
                    group.addTask{
                        var i = j + 1
                        var sum = 0
                        print("starting")
                        while i <= n {
                            sum += A.f(i)
                            i += factor
                        }
                        return sum
                    }
                }
                for await result in group {
                    sum+=result
                }
            }
            return sum
        }
    }
    static func main()async{
        let task = Process()
        task.executableURL = URL(fileURLWithPath: "/usr/sbin/sysctl")
        task.arguments = ["hw.ncpu"]
        let out = Pipe()
        task.standardOutput = out
        let res: ()? = try? task.run()
        if let _ = res {
            let outputData = out.fileHandleForReading.readDataToEndOfFile()
            let s = String(decoding: outputData, as: UTF8.self)
            print("got from sysctl ",s)
            let sep = CharacterSet(charactersIn: " \n")
            let components = s.components(separatedBy: sep)
            factor = Int(components[1]) ?? 4
        }
        print("factor ",factor)
        print("entering main")
        let a = A()
        await print(a.g(100))
        await print(a.g(Int(5e8)))
    }
}


-- 

7-77-777
Evil Sinner!
to weak you should be meek, and you should brainfuck stronger
https://github.com/rofl0r/chaos-pp

Back to comp.programming.threads | Previous | Next | Find similar | Unroll thread


Thread

Problem 512 Euler Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-03 19:05 +0000

csiph-web