Henry did ask.........
OK (Liverpool drew with Man City - the boys are reasonably happy.)
Here's Sa and two subs; it's an "improvement" on S and x3max and x4.
"timer" uses the difference between 6!:0 before and after.
NB. SPOILER alert - an attempt at solving Euler Project Problem 791.
Listings below the spoiler gap.
Cheers,
Mike
Should be ok in fixed width font.
NB. find sum of all non decreasing positive integer quads x1 x2 x3 x4
satisfying p791 specs
Sa =: 3 : 0
n =. y
s =. 0
LISTA =: 0 4$0 NB. stores quadruples satisfying p791 specs
for_a. range 1,n do.
b =. range a,n
if. +/ ok =. 0 < c =. x3maxa a; b do.
b =. ok#b
abc =. a,. b ;@:(([,. range@, )each) ok#c
sabc=. x4 n, abc
s =. s + sabc
LISTA =: LISTA, Y
end.
end.
s
)
NB. reduce search space by finding max possible x3 given x1 & x2
x3maxa =: 3 : 0
'x1 x2' =. y NB. input is X1;X2, scalar; vector
x12 =. *: x1
x22 =. *: x2
b =. 32 * 1 + s =. x1 + x2 NB. YES - I know I could factor
down !!!
c =. 4 + 32 * s + (x1 * x2) - x12 + x22 ok =. , 0 <: dis =.
(*:b) + 128 * c
x2 (<: * ] ) ok * <. _64 %~ b -~ - %: 0 >. dis
)
NB. solve x4 given x1 x2 x3, return sum x if solution is appropriate, ie
x3 <: x4 <: n
x4 =: 3 : 0
n =. y {~ <0 0
y =. }. y
s =. +/"1 y
c =. (2*s) - 3 * +/"1 *:y
'x1 x2 x3' =. |: y
c =. c + 2 * (x1 <.@* x2 + x3) + x2 <.@* x3
b =. 2 (+ +:) s
a2 =. 6
ok =. 0 <: dis=. b (+*:)~ 2*a2<.@*c
b =. ok # b
x4 =. a2 %~ b + %: ok#dis
x3 =. ok#x3
s =. ok#s
y =. ok#y
ok =. x3 (<: * ((<:&n) * (=<.))@]) x4
Y =: ok#y,. x4 NB. debug info
<. +/"1 ok#x4+s
)
On 10/04/2022 17:33, 'Mike Day' via Beta wrote:
Sa’s a bit long-winded.... I’m out at the mo, watching Liverpool with Ben.
It’s providing a solution for project Euler problem 791, still too slow.
More details if you need them in an hour or two,
Cheers,
Mike
Sent from my iPad
On 10 Apr 2022, at 16:37, Henry Rich <[email protected]> wrote:
What is Sa?
Henry Rich
On 4/10/2022 10:58 AM, 'Michael Day' via Beta wrote:
I see plenty of correspondents have had a go already.
However, a few examples of use might be helpful. I have just found
t. & T. in NuVoc, having assumed they wouldn't be there yet, so thanks for
that.
Trying t =: t.'' as suggested elsewhere, and T =: T.&'', in a session with
T each 1 2 3 4 NB. ie 3 threads
+-+-----+-++
|3|3 1 2|0||
+-+-----+-++
I find
timer'Sa 1000' NB. returns correct result for y=1000
+--------+--------+
|0.266998|37048340|
+--------+--------+
timer'Sa t 1000' NB. no time detected
++--------+
||37048340|
++--------+
ts'Sa 1000'
0.273682 481344
ts'Sa t 1000'
1.86e_5 1408
But trying to capture the result of Sa hangs:
ts'q =: Sa t 1000'
.... hourglass ...
in Windows 11. Using the J break batch file doesn't appear to work.
I've now tried timex =: 6!:2 rather than ts =: 6!:2 , 7!:2@]
with a bit more success, which suggests the space query is causing the hang:
timex'q=: Sa 16000' NB. longer time so I can try stopwatching as below
27.1941
erase'q'
1
timex'q=: Sa t 16000' NB. supersonic!?!?
2.41e_5
q NB. no! - this took about 25 seconds to return a boxed value,
from a pyx, I suppose:
+-----------+
|38245553964|
+-----------+
So the session hangs for around 25 seconds while the value for q is found.
(Sa is my best effort so far, and far too slow so far, for Project Euler
Problem 791,
https://projecteuler.net/problem=791 , in case of interest.)
FWIW,
JVERSION
Engine: j904/j64avx2/windows
Beta-a: commercial/2022-04-09T17:57:22
Library: 9.04.01
Qt IDE: 2.0.3/6.2.4(6.2.4)
Platform: Win 64
Installer: J904 install
InstallPath: c:/d/j904-beta
Contact: www.jsoftware.com
Thanks,
Mike
On 09/04/2022 20:04, Henry Rich wrote:
The big news in J904 is multithreading. J verbs can now be run in different
cores, sharing the J global namespaces.
[A version of J with limited support for multithreading was revealed last year
by John Ference of Monument AI. John's work was an important proof of concept,
but the J904 version is an independent development using pthreads instead of
OpenMP. John has agreed with Jsoftware to give Jsoftware access to the code
and testcases used in his work. We have not used the code.]
Two primitives support multithreading:
(0 T. '') creates a thread on a new core. A thread running a verb is called
a /task/. Even if you don't plan to create tasks, you should create threads to
allow JE to use them for its own purposes.
([x] u t. '' y) executes ([x] <@:u y) as a task to produce a boxed result. The
thread that executed t. continues to run. But the result is a special kind of box
called a /pyx/.
['pyx' is Greek for box, and has been brought into English on occasion when a
word has been needed for a special box. The most recent such borrowing until
now is for the box used by the Royal Mint to hold coins for assay.]
A pyx looks on the outside like any other box. You can pass it as an argument.
But as soon as you look inside it, your thread is blocked until the task
producing the pyx's value has completed. Then you see the value, and your
thread continues.
Tasks share the global namespaces and thus can freely read, write, stomp on,
and delete names visible to other tasks.
We encourage you to experiment with threads and tasks. We are sure that a
layer of synchronizing primitives - semaphores, locks, and mutexes - will be
needed, but we would like to get practical experience with tasks before we
implement them. Suggestions welcome.
Henry Rich
--
This email has been checked for viruses by AVG.
https://www.avg.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm