Swift 5.5 introduces actors and async/await

5 views
Skip to first unread message

Branimir Maksimovic

unread,
Jul 21, 2021, 7:16:53 PMJul 21
to
Beautifull :P
This is latest feature, my poblem512 code in Swift 5.5 :p
(you'll need macOS 12 Monterey and XCode 13 beta).

// problem512.swift
// problem512Euler
//
// Created by Branimir Maksimovic on 21.7.21..
//

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.async{
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)))
}
}

bmaxa@Branimirs-Air project_euler % time ./Problem512Euler/bin/Problem512Euler
got from sysctl hw.ncpu: 8

factor 8
entering main
starting
starting
starting
starting
starting
starting
starting
starting
2007
starting
starting
starting
starting
starting
starting
starting
starting
50660591862310323
./Problem512Euler/bin/Problem512Euler 352.89s user 0.63s system 399% cpu 1:28.45 total

yeah.BTW it is much faster to download XCode beta and unzip anywhere then installing from appstore :P


DFS

unread,
Jul 22, 2021, 12:52:29 PMJul 22
to
On 7/21/2021 7:16 PM, Branimir Maksimovic wrote:

> Beautifull :P
> This is latest feature, my poblem512 code in Swift 5.5 :p
> (you'll need macOS 12 Monterey and XCode 13 beta).

snip code


> bmaxa@Branimirs-Air project_euler % time ./Problem512Euler/bin/Problem512Euler
> got from sysctl hw.ncpu: 8
>
> 50660591862310323
> ./Problem512Euler/bin/Problem512Euler 352.89s user 0.63s system 399% cpu 1:28.45 total


Congrats!

Did you know the creator of the site says the problems should be/are
meant to be solved in about one minute?

You have to know some math tricks to do that.





Branimir Maksimovic

unread,
Jul 22, 2021, 6:14:52 PMJul 22
to
That's not the point. You missed.
bmaxa@Branimirs-Air project_euler % time ./problem512
odd prime list size => 24
100 => 2007
odd prime list size => 26355866
500000000 => 50660591862310323
./problem512 6.52s user 0.46s system 98% cpu 7.082 total
I wont give you this solution :P

>
>
>
>


--
bmaxa now listens Paranoid Android by Radiohead from Live Eurockeennes 04-07-2003

dfs

unread,
Jul 22, 2021, 7:46:32 PMJul 22
to
On 7/22/21 6:14 PM, Branimir Maksimovic wrote:
> On 2021-07-22, DFS <nos...@dfs.com> wrote:
>> On 7/21/2021 7:16 PM, Branimir Maksimovic wrote:
>>
>>> Beautifull :P
>>> This is latest feature, my poblem512 code in Swift 5.5 :p
>>> (you'll need macOS 12 Monterey and XCode 13 beta).
>>
>> snip code
>>
>>
>>> bmaxa@Branimirs-Air project_euler % time ./Problem512Euler/bin/Problem512Euler
>>> got from sysctl hw.ncpu: 8
>>>
>>> 50660591862310323
>>> ./Problem512Euler/bin/Problem512Euler 352.89s user 0.63s system 399% cpu 1:28.45 total
>>
>>
>> Congrats!
>>
>> Did you know the creator of the site says the problems should be/are
>> meant to be solved in about one minute?
>>
>> You have to know some math tricks to do that.
>>
>
> That's not the point. You missed.
> bmaxa@Branimirs-Air project_euler % time ./problem512
> odd prime list size => 24
> 100 => 2007
> odd prime list size => 26355866
> 500000000 => 50660591862310323
> ./problem512 6.52s user 0.46s system 98% cpu 7.082 total

Your other post said 352.89s


> I wont give you this solution :P

I didn't ask.

Branimir Maksimovic

unread,
Jul 22, 2021, 7:51:07 PMJul 22
to
This is another solution for 7 secs, but I didn't want to talk about
problem 512 rather about multithreading :P

>
>> I wont give you this solution :P
>
> I didn't ask.
Ok.

--
bmaxa now listens Chants Religieux by Alain Kremsky (piano)
Reply all
Reply to author
Forward
0 new messages