Re: [Jprogramming] Quora Challenge

2017-09-20 Thread 'Mike Day' via Programming
I'm probably missing the point,  but it's perhaps worth observing that you might choose to consider ALL the numbers here (except the exponent) as belonging to the finite field of numbers mod 13,  in which case "Roger's approach" doesn't fail. What about 10^20 ? 13&|@(10&^) 20 9 (in fact,   

Re: [Jprogramming] Quora Challenge

2017-09-20 Thread Raul Miller
Er... ok: yes, not exactly but sort of, and that depends? It's a messy subject, but switching to extended precision is a much more general approach for this kind of problem. Thanks, -- Raul On Wed, Sep 20, 2017 at 5:31 AM, Erling Hellenäs wrote: > Hi all ! > > It

Re: [Jprogramming] Quora Challenge

2017-09-20 Thread Erling Hellenäs
Hi all ! It just surprised me that you didn't mention it as a possible solution to the problems of Skip Cave. Since you did not it seemed there might be a problem with this solution. It might not be a possible solution. That's why I ask these three questions. It's not because I can not read

Re: [Jprogramming] Quora Challenge

2017-09-19 Thread Raul Miller
Look for yourself, http://www.jsoftware.com/help/dictionary/dx009.htm - search for "comparison tolerance". Thanks, -- Raul On Tue, Sep 19, 2017 at 1:15 PM, Erling Hellenäs wrote: > Comparison tolerance is a global setting you can turn off? > It can then be

Re: [Jprogramming] Quora Challenge

2017-09-19 Thread Erling Hellenäs
Comparison tolerance is a global setting you can turn off? It can then be explicitly used with u!.t after verbs? Will it cause a lot of problems with library functions? /Erling On 2017-09-19 14:56, Raul Miller wrote: Hypothetically, you could rig up a 9!:n mechanism (to turn this on / off) and

Re: [Jprogramming] Quora Challenge

2017-09-19 Thread Raul Miller
Hypothetically, you could rig up a 9!:n mechanism (to turn this on / off) and rig up the code that promotes ints to floats print a warning. You'd probably also want some kind of anti-spam measure in there (display the warning only once until the user issues another command line, or something iike

Re: [Jprogramming] Quora Challenge

2017-09-19 Thread Skip Cave
Roger's approach also fails if the numbers are big enough. Here's the original problem, but with the last number changed to protect the innocent (and make the demonstration work) (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198 3 ​Now let's use some big numbers:​ ​ (4 : '13|x*y') /

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Raul Miller
Or, you could do something like this: paren=:3 :0 y=.":y if.1<#;:y do. y=.'(',y,')' end. ;:y ) quoted=: 1 :0 : ;:inv(paren x),(paren m),;:": y ) 'g' quoted/19 29 59 79 89 109 119 139 149 179 199 19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199 Now you have a line you can

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Jimmy Gauvin
To answer "​There's got to be an easier way to list the interim values of insert" g/\.19 29 59 79 89 109 119 139 149 179 199 4 5 6 12 12 7 4 2 6 1 199 To see what \. does we can use < <\.i.5 ┌─┬───┬─┬───┬─┐ │0 1 2 3 4│1 2 3 4│2 3 4│3 4│4│ └─┴───┴─┴───┴─┘ \

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Skip Cave
So does Roger's function work on the larger list? Let's see: (4 : '13|x*y') / 10 # 19 29 59 79 89 109 119 139 149 179 199 9 That looks like it does. Let's check with extended precision: (4 : '13|x*y') / 10 # 19 29 59 79 89 109 119 139 149 179 199x 9 So Roger's approach works without

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Skip Cave
Roger said: But if the list were longer: 13 | */ 13 | 10 # 19 29 59 79 89 109 119 139 149 179 199 0 Wow! Yes I see the problem. It would be really nice if J would output a warning when a calculation exceeds the precision limits. I'm sure this would probably slow down all computations, so

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Roger Hui
Your solution works only because the list isn't too long, and the initial 13| (same as 13 |/) made them all small enough that you can do the */ without losing precision. But if the list were longer: 13 | */ 13 | 10 # 19 29 59 79 89 109 119 139 149 179 199 0 If it makes it more

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Skip Cave
Yes, one of the Quora answer posts for this challenge pointed this out as well, so I tried that approach. Not being too competent with the use of & @, I tried: 13|*/13|/19 29 59 79 89 109 119 139 149 179 199 4 I got the answer, but I need a course on the use of @ and & in order to tighten

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Roger Hui
This particular problem can be solved without resort to extended precision. Use multiplication mod 13, that is, find the remainder at each step rather than waiting until the end: 13&|@* / 19 29 59 79 89 109 119 139 149 179 199 4 On Mon, Sep 18, 2017 at 10:00 AM, Skip Cave

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Skip Cave
People posting problems on Quora are typically expecting an explicit algebraic formula solution to their problem, though they never explicitly state that requirement. The posted Quora problems often involve very large numbers, which the poster expects will prevent solutions from using a

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Raul Miller
That has been characteristic of Project Euler but Quora Challenges are new to me. Anyways, if the problem expects to answers work with precisions well beyond measurable limits, that should be stated as a part of the problem. Thanks, -- Raul On Sun, Sep 17, 2017 at 2:05 PM, Skip Cave

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Erling Hellenäs
I tested with this "primes" function in Nial, but even my first attempt at generating primes in J, The PrimesUntil verb, is considerably better. At 1 primes Nial got in trouble. At 2 primes Nial crashed. https://en.wikipedia.org/wiki/Nial#Explanation /Erling Den 2017-09-17 kl.

Re: [Jprogramming] Quora Challenge

2017-09-18 Thread Erling Hellenäs
ECPP is the best algorithm if you want proof of a correct result for any large number? https://en.wikipedia.org/wiki/Elliptic_curve_primality /Erling Den 2017-09-17 kl. 22:30, skrev Roger Hui: In theory Miller-Rabin can give incorrect result, but the probability of that is lower than the

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
​Since the Quora Challenge is a question on intervals it seems some sort of sieve calculation would be most efficient, because sieving means you don't have do start from scratch for each number in the interval. About 8 years ago, I designed and implemented a model of p: in Dyalog APL, coded

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
> I have some thoughts on solving this Quora Challenge which I will post in a bit. First, a teaser, computed completely in Dyalog APL with no additional C coding. t←(1e6+2e9) pc 2e6+2e9 ⍴t 185 5↑t 2001401814 2001401815 2001401816 2001401817 2001401818 ¯5↑t 2001401994

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
In theory Miller-Rabin can give incorrect result, but the probability of that is lower than the probability of a cosmic ray particle impacting a circuit in your CPU and causing it to give a wrong answer. I have some thoughts on solving this Quora Challenge which I will post in a bit. On Sun,

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
"Currently, arguments larger than2^31are tested to be prime according to a probabilistic algorithm (Miller-Rabin)" http://www.jsoftware.com/docs/help804/dictionary/dpco.htm Miller–Rabin primality test https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test This means that sometimes,

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
It failed  on the commented tests, but it's dead fast, yes. /Erling s=: 10 f 100 s >./90 91 92 93 94 95 96 = s NB. s=: 1 f 2 NB. >./s = 1 NB.s=: 2 f 3 NB. 0 = $ s NB. s=: 8 f 9 NB. >./s = 8 9 s=: 7 f 8 >./s = 8 NB.s=: 10 f 11 NB.>./s = 10 s=: 7 f 11 >./s = 8 9 10 s=: 200 f 300 s

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Skip Cave
The fact that the problem is a Quora challenge is exactly the point. It's like if someone asks your for the square root of 75976307044. Yes, there is a manual process that one can use to compute the square root. But why spend all that time when one can type %:75976307044 or use a hand-held

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
Oops, biggap=:{~0 1+[:(i.<./)2-/\] Thanks, -- Raul On Sunday, September 17, 2017, Erling Hellenäs wrote: > Strange things happen here. It works but give incorrect results. I just > changed F to lower case to put it through my tests. Not sure what might >

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
I paste my submission into a new temp, run it in a clean terminal and I see no problems. /Erling On 2017-09-17 17:20, Raul Miller wrote: Ok, thanks - with these additional definitions, your f works for me. Thanks, -- For

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
Strange things happen here. It works but give incorrect results. I just changed F to lower case to put it through my tests. Not sure what might happen here. I have J 8.04 on this machine. 64 bit. I never doubted that p: was good. I just thought that since it was a Quora challenge that using p:

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
Yes. :) When J provides a mechanism for something it is usually worth trying. Sometimes you can write something faster, but usually J's approach will be useful. For example, in this case, consider this approach: thru=: <. + >:@>. i.@- <. biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ] F=: [:

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
Ok, thanks - with these additional definitions, your f works for me. Thanks, -- Raul On Sun, Sep 17, 2017 at 10:05 AM, Erling Hellenäs wrote: > Hi Raul ! > > This is what happens when I run it in a clean JQT. I added a timespecex > call. Not sure what might happen

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
My first version easily handles 20 000 000. And I thought the second would be faster. Lol. J seems to have a very efficient way to see if a number is a prime. timespacex'2 f 2000' 12.2883 5.36873e8 /Erling On 2017-09-17 16:22, Erling Hellenäs wrote: It takes quite some time at 20

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
It takes quite some time at 20 and more. Due to PrimesUntil, it seems. timespacex'2 f 2000' 0.0121769 42240 timespacex'2 f 2' 0.426833 533760 timespacex'2 f 20' 30.0596 4.20378e6 timespacex'PrimesUntil 20' 30.0592 4.19981e6 /Erling  On 2017-09-17 16:05, Erling Hellenäs

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
Hi Raul ! This is what happens when I run it in a clean JQT. I added a timespecex call. Not sure what might happen for you. Cheers, Erling Prime=: [: -. [: +./0 = [ | ] (,2) Prime 3 1 NB. 1 2 3 Prime 4 0 NB. 0 2 3 Prime 5 1 NB. 1 2 3 5 Prime 6 0 NB. 0 PrimesUntil=: 3 : 0

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
I was going to look at 200 f 300 and then try timespacex between your f and other implementations of f, but I got a value error on AddStartRangeIfNotThere FYI, -- Raul On Sun, Sep 17, 2017 at 8:23 AM, Erling Hellenäs wrote: > Hi all! > > Here is a version which does

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
Hi all! Here is a version which does not use the p: verb. It is more useful as description of the algorithm. It is also probably less wasteful of computer resources. Cheers, Erling Prime=: [: -. [: +./0 = [ | ] (,2) Prime 3 NB. 1 2 3 Prime 4 NB. 0 2 3 Prime 5 NB. 1 2 3 5 Prime 6

Re: [Jprogramming] Quora Challenge

2017-09-16 Thread Erling Hellenäs
Hi all ! My submission, see below. Cheers, Erling Hellenäs RangeIndex=: ([ + [: i. ] - [) , ] 3 RangeIndex 11 NB. 3 4 5 6 7 8 9 10 11 8 RangeIndex 10 NB. 8 9 10 Primes=: (1 p: ]) # ] Primes 3 4 5 6 7 8 9 10 11 NB. 3 5 7 11 Primes 8 9 10 NB. i.0 AddStartRangeIfNotThere=: (([ ~: 1 {.

Re: [Jprogramming] Quora Challenge

2017-09-16 Thread Louis de Forcrand
lnps=: 4 : 0 (i{p)+}.i.d{~i=. (i.>./)d=. 2-~/\p=. (<:x),(p:([+i.@-~)/_1 p:x,y),>:y ) Cheers, Louis > On 15 Sep 2017, at 18:24, Raul Miller wrote: > > Oh, oops... > > thru=: <. + i.@(+ *@+&0.5)@-~ > biggap=: {~ (0 1 + [: (i. >./) 2 -~/\ ]) > f=: [: thru/ 1 _1 + [: biggap

Re: [Jprogramming] Quora Challenge

2017-09-15 Thread Raul Miller
Oh, oops... thru=: <. + i.@(+ *@+&0.5)@-~ biggap=: {~ (0 1 + [: (i. >./) 2 -~/\ ]) f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv) Thanks, -- Raul On Fri, Sep 15, 2017 at 12:08 PM, Skip Cave wrote: > Something is wrong with Raul's 'f' > >10 f 100 > > 90

Re: [Jprogramming] Quora Challenge

2017-09-15 Thread Skip Cave
Something is wrong with Raul's 'f' 10 f 100 90 91 92 93 94 95 96 100 f 200 114 115 116 117 118 119 120 121 122 123 124 125 126 200 f 300 294 295 296 297 298 299 300 301 302 303 304 305 306 That last list isn't in the interval 200, 300 Skip On Fri, Sep 15, 2017 at 7:40 AM, Raul

Re: [Jprogramming] Quora Challenge

2017-09-15 Thread Jan-Pieter Jacobs
I came up with this one: primerun=:( [: longbox 1: <;._1 ])@to to=: [ + i.@:>:@-~ longbox =: (>@#~ (=>./)@:(#&>)) Best regards, Jan-Pieter On 15 Sep 2017 11:36, "Skip Cave" wrote: > Write a function f, which will list the longest set of consecutive >

Re: [Jprogramming] Quora Challenge

2017-09-15 Thread Raul Miller
Do you mean like this? thru=: <. + i.@(+ *@+&0.5)@-~ biggap=: {~ (0 1 + [: (i. >./) 2 -~/\ ]) f=: [: thru/ 1 _1 + [: biggap thru&.(p:inv) Thanks, -- Raul On Fri, Sep 15, 2017 at 5:36 AM, Skip Cave wrote: > Write a function f, which will list the longest set of