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,
   13&|@(10&^) 8 20 32 44  NB. same for all exponents mod 12 (!)

9 9 9 9   )


   13|10^20x  NB. cf extended

9


(13&|@*/) 19 29 59 79 89 109 119 139 149 179 198  NB. original exercise?
19

   (13&|@*/) 19 29 59 79 89 109 119 139 149 179 198 * 13&|@(10&^) 20
9

Note also:
(13&|@*/) (13 | 19 29 59 79 89 109 119 139 149 179 198) * 13&|@(10&^) 20
9

IF the exponent is itself large-ish,  eg 10^300,  we can use Euler's 
theorem, qv.
which, in this case, leads to 10^400 mod 13 being equivalent to 
10^12|400,  mod 13.


Easier of course to just exploit extended integers, provided memory 
problems don't

preclude them.

Mike


PS - sorry if the layout breaks down - looks ok on sending!


On 19/09/2017 07:14, Skip Cave wrote:

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') / 19 29 59 79 89 109 119 139 149 179 198 + 10^20
0​

But if we use extended precision:

  (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1


Showing interim results:

  (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1 7 6 2 8 3 3 5 1 7 10198


For that matter, my original approach will work with the same big numbers
as long as you use extended precision:

13|*/13|/ 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1


However, 0 can be a valid result in these remainder problems.  Notice the
changed last digit (195 is a multiple of 13):

  (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 195

0


 (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 195

0 0 0 0 0 0 0 0 0 0 195


Any number in the list which is a multiple of 13, will cause the final
answer to be zero.


So you can't rely on a zero answer to indicate a "precision exceeded"
error. Which is why it would be nice to have a way to be notified when you
are exceeding the precision limit.


Skip


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 3:48 PM, Skip Cave  wrote:


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 extended precision. But mine doesn't:


13| */13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199

0

Let's see why. Here's Roger's verb. We'll call it 'g':

g=. 4 : '13|x*y'

Roger does g/ which puts g between each integer. Let's see what is going
on In the short case:

  19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199

4


Step-by-step...


 179 g 199

1

 149 g 1

6

 139 g 6

2

 119 g 2

4

 109 g 4

7

  89 g 7

12

  79 g 12

12

  59 g 12

6

  29 g 6

5

 19 g 5

4

So the secret is that Roger's scheme keeps the interim calculation values
small enough to avoid precision errors. (There's got to be an easier way to
list the interim values of insert). My scheme generated large interim
values which exceeded the precision limit.

Skip


I get a set of numbers from a database. The

Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 2:51 PM, Skip Cave 
wrote:


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 having a global variable to
turn on precision errors would be a conservative approach.

Of course, I can usually check to see if an error has occurred by turning
on extended precision to see if the answers match, but it would be nice to
know without having to always double check my calculations..

13|*/13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199x

9


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 1:46 PM, Roger Hui 
wrote:


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 understandable, use explicit defn:

(4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 199
4

"Multiplication mod m" means m|x*y, multiply, then take the remainder of
division by m.






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 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 or have not
> read the documentation.
>
> 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?
>
> Cheers,
>
> Erling
>
>
>
> Den 2017-09-19 kl. 20:10, skrev Raul Miller:
>>
>> Look for yourself, http://www.jsoftware.com/help/dictionary/dx009.htm
>> - search for "comparison tolerance".
>>
>> Thanks,
>>
>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 
or have not read the documentation.


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?

Cheers,

Erling



Den 2017-09-19 kl. 20:10, skrev Raul Miller:

Look for yourself, http://www.jsoftware.com/help/dictionary/dx009.htm
- search for "comparison tolerance".

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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 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 that). Then all you need to do is test to make
>> sure you haven't broken anything and deal with deploying these
>> changes.
>>
>> But there's another way, and it's available right now:
>>
>> datatype (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198
>> integer
>> datatype  (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 +
>> 10^20
>> floating
>>
>> If your result is floating and you meant for it to be integer, switch
>> to extended and see if it changes.
>>
>> That said, if you're working with large numbers (if your numbers can
>> ever be more than 15 digits), and this kind of precision matters for
>> you, you should always be working with extended precision values.
>>
>> Thanks,
>>
>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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 that). Then all you need to do is test to make
sure you haven't broken anything and deal with deploying these
changes.

But there's another way, and it's available right now:

datatype (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198
integer
datatype  (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 + 10^20
floating

If your result is floating and you meant for it to be integer, switch
to extended and see if it changes.

That said, if you're working with large numbers (if your numbers can
ever be more than 15 digits), and this kind of precision matters for
you, you should always be working with extended precision values.

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm

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 that). Then all you need to do is test to make
sure you haven't broken anything and deal with deploying these
changes.

But there's another way, and it's available right now:

   datatype (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198
integer
   datatype  (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 + 10^20
floating

If your result is floating and you meant for it to be integer, switch
to extended and see if it changes.

That said, if you're working with large numbers (if your numbers can
ever be more than 15 digits), and this kind of precision matters for
you, you should always be working with extended precision values.

Thanks,

-- 
Raul


On Tue, Sep 19, 2017 at 2:14 AM, Skip Cave  wrote:
> 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') / 19 29 59 79 89 109 119 139 149 179 198 + 10^20
> 0
>
> But if we use extended precision:
>
>  (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198 + 10x^20
>
> 1
>
>
> Showing interim results:
>
>  (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 + 10x^20
>
> 1 7 6 2 8 3 3 5 1 7 10198
>
>
> For that matter, my original approach will work with the same big numbers
> as long as you use extended precision:
>
>13|*/13|/ 19 29 59 79 89 109 119 139 149 179 198 + 10x^20
>
> 1
>
>
> However, 0 can be a valid result in these remainder problems.  Notice the
> changed last digit (195 is a multiple of 13):
>
>  (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 195
>
> 0
>
>
> (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 195
>
> 0 0 0 0 0 0 0 0 0 0 195
>
>
> Any number in the list which is a multiple of 13, will cause the final
> answer to be zero.
>
>
> So you can't rely on a zero answer to indicate a "precision exceeded"
> error. Which is why it would be nice to have a way to be notified when you
> are exceeding the precision limit.
>
>
> Skip
>
>
> Skip Cave
> Cave Consulting LLC
>
> On Mon, Sep 18, 2017 at 3:48 PM, Skip Cave  wrote:
>
>> 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 extended precision. But mine doesn't:
>>
>>
>>13| */13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199
>>
>> 0
>>
>> Let's see why. Here's Roger's verb. We'll call it 'g':
>>
>>g=. 4 : '13|x*y'
>>
>> Roger does g/ which puts g between each integer. Let's see what is going
>> on In the short case:
>>
>>  19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199
>>
>> 4
>>
>>
>> Step-by-step...
>>
>>
>> 179 g 199
>>
>> 1
>>
>> 149 g 1
>>
>> 6
>>
>> 139 g 6
>>
>> 2
>>
>> 119 g 2
>>
>> 4
>>
>> 109 g 4
>>
>> 7
>>
>>  89 g 7
>>
>> 12
>>
>>  79 g 12
>>
>> 12
>>
>>  59 g 12
>>
>> 6
>>
>>  29 g 6
>>
>> 5
>>
>> 19 g 5
>>
>> 4
>>
>> So the secret is that Roger's scheme keeps the interim calculation values
>> small enough to avoid precision errors. (There's got to be an easier way to
>> list the interim values of insert). My scheme generated large interim
>> values which exceeded the precision limit.
>>
>> Skip
>>
>>
>> I get a set of numbers from a database. The
>>
>> Skip Cave
>> Cave Consulting LLC
>>
>> On Mon, Sep 18, 2017 at 2:51 PM, Skip Cave 
>> wrote:
>>
>>> 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 having a global variable to
>>> turn on precision errors would be a conservative approach.
>>>
>>> Of course, I can usually check to see if an error has occurred by turning
>>> on extended precision to see if the answers match, but it would be nice to
>>> know without having to always double check my calculations..
>>>
>>>13|*/13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199x
>>>
>>> 9
>>>
>>>
>>> Skip Cave
>>> Cave Consulting LLC
>>>
>>> On Mon, Sep 18, 2017 at 1:46 PM, Roger Hui 
>>> wrote:
>>>
 Your solution works only because the list isn't too long, and the 

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') / 19 29 59 79 89 109 119 139 149 179 198 + 10^20
0​

But if we use extended precision:

 (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1


Showing interim results:

 (4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1 7 6 2 8 3 3 5 1 7 10198


For that matter, my original approach will work with the same big numbers
as long as you use extended precision:

   13|*/13|/ 19 29 59 79 89 109 119 139 149 179 198 + 10x^20

1


However, 0 can be a valid result in these remainder problems.  Notice the
changed last digit (195 is a multiple of 13):

 (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 195

0


(4 : '13|x*y') /\. 19 29 59 79 89 109 119 139 149 179 195

0 0 0 0 0 0 0 0 0 0 195


Any number in the list which is a multiple of 13, will cause the final
answer to be zero.


So you can't rely on a zero answer to indicate a "precision exceeded"
error. Which is why it would be nice to have a way to be notified when you
are exceeding the precision limit.


Skip


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 3:48 PM, Skip Cave  wrote:

> 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 extended precision. But mine doesn't:
>
>
>13| */13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199
>
> 0
>
> Let's see why. Here's Roger's verb. We'll call it 'g':
>
>g=. 4 : '13|x*y'
>
> Roger does g/ which puts g between each integer. Let's see what is going
> on In the short case:
>
>  19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199
>
> 4
>
>
> Step-by-step...
>
>
> 179 g 199
>
> 1
>
> 149 g 1
>
> 6
>
> 139 g 6
>
> 2
>
> 119 g 2
>
> 4
>
> 109 g 4
>
> 7
>
>  89 g 7
>
> 12
>
>  79 g 12
>
> 12
>
>  59 g 12
>
> 6
>
>  29 g 6
>
> 5
>
> 19 g 5
>
> 4
>
> So the secret is that Roger's scheme keeps the interim calculation values
> small enough to avoid precision errors. (There's got to be an easier way to
> list the interim values of insert). My scheme generated large interim
> values which exceeded the precision limit.
>
> Skip
>
>
> I get a set of numbers from a database. The
>
> Skip Cave
> Cave Consulting LLC
>
> On Mon, Sep 18, 2017 at 2:51 PM, Skip Cave 
> wrote:
>
>> 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 having a global variable to
>> turn on precision errors would be a conservative approach.
>>
>> Of course, I can usually check to see if an error has occurred by turning
>> on extended precision to see if the answers match, but it would be nice to
>> know without having to always double check my calculations..
>>
>>13|*/13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199x
>>
>> 9
>>
>>
>> Skip Cave
>> Cave Consulting LLC
>>
>> On Mon, Sep 18, 2017 at 1:46 PM, Roger Hui 
>> wrote:
>>
>>> 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 understandable, use explicit defn:
>>>
>>>(4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 199
>>> 4
>>>
>>> "Multiplication mod m" means m|x*y, multiply, then take the remainder of
>>> division by m.
>>>
>>>
>>>
>>>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 edit - remove however much you want - and
then execute...

Or, if that's too much effort, you could just load'debug/dissect' and
then run dissect on the original sentence.

Thanks,

-- 
Raul


On Mon, Sep 18, 2017 at 10:58 PM, Jimmy Gauvin  wrote:
> 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│
> └─┴───┴─┴───┴─┘
>
> \ is usefull too:
>
>+/\i.5
> 0 1 3 6 10
>
><\i.5
> ┌─┬───┬─┬───┬─┐
> │0│0 1│0 1 2│0 1 2 3│0 1 2 3 4│
> └─┴───┴─┴───┴─┘
>
>
>
> On Mon, Sep 18, 2017 at 4:48 PM, Skip Cave  wrote:
>
>>
>> Roger does g/ which puts g between each integer. Let's see what is going on
>> In the short case:
>>
>>  19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199
>>
>> 4
>>
>> Step-by-step...
>>
>> 179 g 199
>> 1
>> 149 g 1
>> 6
>>
>
> o
> o
> o
>
>> 19 g 5
>> 4
>>
>> So the secret is that Roger's scheme keeps the interim calculation values
>> small enough to avoid precision errors.
>>
>> (There's got to be an easier way to
>> list the interim values of insert). My scheme generated large interim
>> values which exceeded the precision limit.
>>
>> Skip>
>>
>>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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│
└─┴───┴─┴───┴─┘

\ is usefull too:

   +/\i.5
0 1 3 6 10

   <\i.5
┌─┬───┬─┬───┬─┐
│0│0 1│0 1 2│0 1 2 3│0 1 2 3 4│
└─┴───┴─┴───┴─┘



On Mon, Sep 18, 2017 at 4:48 PM, Skip Cave  wrote:

>
> Roger does g/ which puts g between each integer. Let's see what is going on
> In the short case:
>
>  19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199
>
> 4
>
> Step-by-step...
>
> 179 g 199
> 1
> 149 g 1
> 6
>   ​

​o
o
o​

> 19 g 5
> 4
>
> So the secret is that Roger's scheme keeps the interim calculation values
> small enough to avoid precision errors.
> ​​
> (There's got to be an easier way to
> list the interim values of insert). My scheme generated large interim
> values which exceeded the precision limit.
>
> Skip>
>
>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 extended precision. But mine doesn't:


   13| */13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199

0

Let's see why. Here's Roger's verb. We'll call it 'g':

   g=. 4 : '13|x*y'

Roger does g/ which puts g between each integer. Let's see what is going on
In the short case:

 19 g 29 g 59 g 79 g 89 g 109 g 119 g 139 g 149 g 179 g 199

4


Step-by-step...


179 g 199

1

149 g 1

6

139 g 6

2

119 g 2

4

109 g 4

7

 89 g 7

12

 79 g 12

12

 59 g 12

6

 29 g 6

5

19 g 5

4

So the secret is that Roger's scheme keeps the interim calculation values
small enough to avoid precision errors. (There's got to be an easier way to
list the interim values of insert). My scheme generated large interim
values which exceeded the precision limit.

Skip


I get a set of numbers from a database. The

Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 2:51 PM, Skip Cave  wrote:

> 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 having a global variable to
> turn on precision errors would be a conservative approach.
>
> Of course, I can usually check to see if an error has occurred by turning
> on extended precision to see if the answers match, but it would be nice to
> know without having to always double check my calculations..
>
>13|*/13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199x
>
> 9
>
>
> Skip Cave
> Cave Consulting LLC
>
> On Mon, Sep 18, 2017 at 1:46 PM, Roger Hui 
> wrote:
>
>> 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 understandable, use explicit defn:
>>
>>(4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 199
>> 4
>>
>> "Multiplication mod m" means m|x*y, multiply, then take the remainder of
>> division by m.
>>
>>
>>
>>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 having a global variable to
turn on precision errors would be a conservative approach.

Of course, I can usually check to see if an error has occurred by turning
on extended precision to see if the answers match, but it would be nice to
know without having to always double check my calculations..

   13|*/13|/ 10 # 19 29 59 79 89 109 119 139 149 179 199x

9


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 1:46 PM, Roger Hui 
wrote:

> 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 understandable, use explicit defn:
>
>(4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 199
> 4
>
> "Multiplication mod m" means m|x*y, multiply, then take the remainder of
> division by m.
>
>
>
>
>
>
>
> On Mon, Sep 18, 2017 at 11:37 AM, Skip Cave 
> wrote:
>
> > 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 up my code.
> >
> >
> > Skip
> >
> >
> > Skip Cave
> > Cave Consulting LLC
> >
> > On Mon, Sep 18, 2017 at 12:47 PM, Roger Hui 
> > wrote:
> >
> > > 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 
> > > wrote:
> > >
> > > > 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
> > > > calculator or computer program to solve. In most cases, J's extended
> > > > precision arithmetic can handle those large numbers quite handily.
> > > >
> > > > Typical Quora problem:
> > > > How would you calculate the remainder of {19 × 29 × 59 × 79 × 89 ×
> 109
> > ×
> > > > 119 × 139 × 149 × 179 × 199} divided by 13?
> > > >  > > > remainder-of-19-%C3%97-29-%C3%97-59-%C3%97-79-%C3%97-89-%C3%
> > > > 97-109-%C3%97-119-%C3%97-139-%C3%97-149-%C3%97-179-%C3%97-
> > > > 199-divided-by-13>
> > > >
> > > > My answer:
> > > >
> > > > Using the J programming language (Jsoftware.com)
> > > >
> > > > 13|*/19 29 59 79 89 109 119 139 149 179 199x
> > > >
> > > > 4
> > > >
> > > > So the answer is 4
> > > >
> > > >
> > > > Skip Cave
> > > > Cave Consulting LLC
> > > >
> > > > On Mon, Sep 18, 2017 at 6:17 AM, Raul Miller 
> > > > wrote:
> > > >
> > > > > 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 <
> s...@caveconsulting.com>
> > > > > wrote:
> > > > > > 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
> > > > > > calculator, and get the answer instantly. As an engineer, speed
> and
> > > > > > accuracy are more important than going through a complicated
> manual
> > > > > process
> > > > > > to get that same answer.
> > > > > >
> > > > > > I post lots of these J solutions on Quora to remind folks of that
> > > fact.
> > > > > > It's not that I'm against knowing the manual approach to the
> > > solutions.
> > > > > > It's just that the more tools we have to make hard problems
> easier,
> > > the
> > > > > > better we can solve even harder problems.
> > > > > >
> > > > > > Skip Cave
> > > > > > Cave Consulting LLC
> 

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 understandable, use explicit defn:

   (4 : '13|x*y') / 19 29 59 79 89 109 119 139 149 179 199
4

"Multiplication mod m" means m|x*y, multiply, then take the remainder of
division by m.







On Mon, Sep 18, 2017 at 11:37 AM, Skip Cave  wrote:

> 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 up my code.
>
>
> Skip
>
>
> Skip Cave
> Cave Consulting LLC
>
> On Mon, Sep 18, 2017 at 12:47 PM, Roger Hui 
> wrote:
>
> > 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 
> > wrote:
> >
> > > 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
> > > calculator or computer program to solve. In most cases, J's extended
> > > precision arithmetic can handle those large numbers quite handily.
> > >
> > > Typical Quora problem:
> > > How would you calculate the remainder of {19 × 29 × 59 × 79 × 89 × 109
> ×
> > > 119 × 139 × 149 × 179 × 199} divided by 13?
> > >  > > remainder-of-19-%C3%97-29-%C3%97-59-%C3%97-79-%C3%97-89-%C3%
> > > 97-109-%C3%97-119-%C3%97-139-%C3%97-149-%C3%97-179-%C3%97-
> > > 199-divided-by-13>
> > >
> > > My answer:
> > >
> > > Using the J programming language (Jsoftware.com)
> > >
> > > 13|*/19 29 59 79 89 109 119 139 149 179 199x
> > >
> > > 4
> > >
> > > So the answer is 4
> > >
> > >
> > > Skip Cave
> > > Cave Consulting LLC
> > >
> > > On Mon, Sep 18, 2017 at 6:17 AM, Raul Miller 
> > > wrote:
> > >
> > > > 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 
> > > > wrote:
> > > > > 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
> > > > > calculator, and get the answer instantly. As an engineer, speed and
> > > > > accuracy are more important than going through a complicated manual
> > > > process
> > > > > to get that same answer.
> > > > >
> > > > > I post lots of these J solutions on Quora to remind folks of that
> > fact.
> > > > > It's not that I'm against knowing the manual approach to the
> > solutions.
> > > > > It's just that the more tools we have to make hard problems easier,
> > the
> > > > > better we can solve even harder problems.
> > > > >
> > > > > Skip Cave
> > > > > Cave Consulting LLC
> > > > >
> > > > > On Sun, Sep 17, 2017 at 12:28 PM, Erling Hellenäs <
> > > > erl...@erlinghellenas.se>
> > > > > 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
> > > > >> 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: would not be a good idea, since the
> > algorithm
> > > > used
> > > > >> in p: is not known to the audience.  It also seemed like some kind
> > of
> > > > >> cheating, like using a subroutine someone else wrote to solve half
> > the
> > > > >> problem.
> > > > >> Well, I also thought p: would have to generate the array of primes
> > for
> > > > >> each call, but it's obviously not the case. There is a formula
> which
> > > > does
> > > > >> the trick? Or a hash table of primes?
> > > > >>
> > > > >> thru=: <. + >:@>. i.@- <.
> > > > 

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 up my code.


Skip


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 12:47 PM, Roger Hui 
wrote:

> 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 
> wrote:
>
> > 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
> > calculator or computer program to solve. In most cases, J's extended
> > precision arithmetic can handle those large numbers quite handily.
> >
> > Typical Quora problem:
> > How would you calculate the remainder of {19 × 29 × 59 × 79 × 89 × 109 ×
> > 119 × 139 × 149 × 179 × 199} divided by 13?
> >  > remainder-of-19-%C3%97-29-%C3%97-59-%C3%97-79-%C3%97-89-%C3%
> > 97-109-%C3%97-119-%C3%97-139-%C3%97-149-%C3%97-179-%C3%97-
> > 199-divided-by-13>
> >
> > My answer:
> >
> > Using the J programming language (Jsoftware.com)
> >
> > 13|*/19 29 59 79 89 109 119 139 149 179 199x
> >
> > 4
> >
> > So the answer is 4
> >
> >
> > Skip Cave
> > Cave Consulting LLC
> >
> > On Mon, Sep 18, 2017 at 6:17 AM, Raul Miller 
> > wrote:
> >
> > > 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 
> > > wrote:
> > > > 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
> > > > calculator, and get the answer instantly. As an engineer, speed and
> > > > accuracy are more important than going through a complicated manual
> > > process
> > > > to get that same answer.
> > > >
> > > > I post lots of these J solutions on Quora to remind folks of that
> fact.
> > > > It's not that I'm against knowing the manual approach to the
> solutions.
> > > > It's just that the more tools we have to make hard problems easier,
> the
> > > > better we can solve even harder problems.
> > > >
> > > > Skip Cave
> > > > Cave Consulting LLC
> > > >
> > > > On Sun, Sep 17, 2017 at 12:28 PM, Erling Hellenäs <
> > > erl...@erlinghellenas.se>
> > > > 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
> > > >> 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: would not be a good idea, since the
> algorithm
> > > used
> > > >> in p: is not known to the audience.  It also seemed like some kind
> of
> > > >> cheating, like using a subroutine someone else wrote to solve half
> the
> > > >> problem.
> > > >> Well, I also thought p: would have to generate the array of primes
> for
> > > >> each call, but it's obviously not the case. There is a formula which
> > > does
> > > >> the trick? Or a hash table of primes?
> > > >>
> > > >> thru=: <. + >:@>. i.@- <.
> > > >>
> > > >> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
> > > >>
> > > >> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
> > > >>
> > > >> s=: 10 f 100
> > > >>
> > > >> s
> > > >>
> > > >> 20 21 22
> > > >>
> > > >> >./90 91 92 93 94 95 96 = s
> > > >>
> > > >> |length error: scriptd
> > > >>
> > > >> | >./90 91 92 93 94 95 96 =s
> > > >>
> > > >> |[-5] j:\j64-803-user\projects\quoraraul.ijs
> > > >>
> > > >> inv
> > > >>
> > > >> ^:_1
> > > >>
> > > >>
> > > >> Cheers,
> > > >>
> > > >> Erling
> > > >>
> > > >> On 2017-09-17 17:38, Raul Miller wrote:
> > > >>
> > > >>> 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 

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  wrote:

> 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
> calculator or computer program to solve. In most cases, J's extended
> precision arithmetic can handle those large numbers quite handily.
>
> Typical Quora problem:
> How would you calculate the remainder of {19 × 29 × 59 × 79 × 89 × 109 ×
> 119 × 139 × 149 × 179 × 199} divided by 13?
>  remainder-of-19-%C3%97-29-%C3%97-59-%C3%97-79-%C3%97-89-%C3%
> 97-109-%C3%97-119-%C3%97-139-%C3%97-149-%C3%97-179-%C3%97-
> 199-divided-by-13>
>
> My answer:
>
> Using the J programming language (Jsoftware.com)
>
> 13|*/19 29 59 79 89 109 119 139 149 179 199x
>
> 4
>
> So the answer is 4
>
>
> Skip Cave
> Cave Consulting LLC
>
> On Mon, Sep 18, 2017 at 6:17 AM, Raul Miller 
> wrote:
>
> > 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 
> > wrote:
> > > 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
> > > calculator, and get the answer instantly. As an engineer, speed and
> > > accuracy are more important than going through a complicated manual
> > process
> > > to get that same answer.
> > >
> > > I post lots of these J solutions on Quora to remind folks of that fact.
> > > It's not that I'm against knowing the manual approach to the solutions.
> > > It's just that the more tools we have to make hard problems easier, the
> > > better we can solve even harder problems.
> > >
> > > Skip Cave
> > > Cave Consulting LLC
> > >
> > > On Sun, Sep 17, 2017 at 12:28 PM, Erling Hellenäs <
> > erl...@erlinghellenas.se>
> > > 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
> > >> 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: would not be a good idea, since the algorithm
> > used
> > >> in p: is not known to the audience.  It also seemed like some kind of
> > >> cheating, like using a subroutine someone else wrote to solve half the
> > >> problem.
> > >> Well, I also thought p: would have to generate the array of primes for
> > >> each call, but it's obviously not the case. There is a formula which
> > does
> > >> the trick? Or a hash table of primes?
> > >>
> > >> thru=: <. + >:@>. i.@- <.
> > >>
> > >> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
> > >>
> > >> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
> > >>
> > >> s=: 10 f 100
> > >>
> > >> s
> > >>
> > >> 20 21 22
> > >>
> > >> >./90 91 92 93 94 95 96 = s
> > >>
> > >> |length error: scriptd
> > >>
> > >> | >./90 91 92 93 94 95 96 =s
> > >>
> > >> |[-5] j:\j64-803-user\projects\quoraraul.ijs
> > >>
> > >> inv
> > >>
> > >> ^:_1
> > >>
> > >>
> > >> Cheers,
> > >>
> > >> Erling
> > >>
> > >> On 2017-09-17 17:38, Raul Miller wrote:
> > >>
> > >>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
> > >>>
> > >>> There will be cases where another approach could be more efficient,
> > >>> but it performs reasonably well.
> > >>>
> > >>> Thanks,
> > >>>
> > >>>
> > >> 
> --
> > >> For information about J forums see http://www.jsoftware.com/
> forums.htm
> > >>
> > > --
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > 

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
calculator or computer program to solve. In most cases, J's extended
precision arithmetic can handle those large numbers quite handily.

Typical Quora problem:
How would you calculate the remainder of {19 × 29 × 59 × 79 × 89 × 109 ×
119 × 139 × 149 × 179 × 199} divided by 13?


My answer:

Using the J programming language (Jsoftware.com)

13|*/19 29 59 79 89 109 119 139 149 179 199x

4

So the answer is 4


Skip Cave
Cave Consulting LLC

On Mon, Sep 18, 2017 at 6:17 AM, Raul Miller  wrote:

> 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 
> wrote:
> > 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
> > calculator, and get the answer instantly. As an engineer, speed and
> > accuracy are more important than going through a complicated manual
> process
> > to get that same answer.
> >
> > I post lots of these J solutions on Quora to remind folks of that fact.
> > It's not that I'm against knowing the manual approach to the solutions.
> > It's just that the more tools we have to make hard problems easier, the
> > better we can solve even harder problems.
> >
> > Skip Cave
> > Cave Consulting LLC
> >
> > On Sun, Sep 17, 2017 at 12:28 PM, Erling Hellenäs <
> erl...@erlinghellenas.se>
> > 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
> >> 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: would not be a good idea, since the algorithm
> used
> >> in p: is not known to the audience.  It also seemed like some kind of
> >> cheating, like using a subroutine someone else wrote to solve half the
> >> problem.
> >> Well, I also thought p: would have to generate the array of primes for
> >> each call, but it's obviously not the case. There is a formula which
> does
> >> the trick? Or a hash table of primes?
> >>
> >> thru=: <. + >:@>. i.@- <.
> >>
> >> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
> >>
> >> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
> >>
> >> s=: 10 f 100
> >>
> >> s
> >>
> >> 20 21 22
> >>
> >> >./90 91 92 93 94 95 96 = s
> >>
> >> |length error: scriptd
> >>
> >> | >./90 91 92 93 94 95 96 =s
> >>
> >> |[-5] j:\j64-803-user\projects\quoraraul.ijs
> >>
> >> inv
> >>
> >> ^:_1
> >>
> >>
> >> Cheers,
> >>
> >> Erling
> >>
> >> On 2017-09-17 17:38, Raul Miller wrote:
> >>
> >>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
> >>>
> >>> There will be cases where another approach could be more efficient,
> >>> but it performs reasonably well.
> >>>
> >>> Thanks,
> >>>
> >>>
> >> --
> >> For information about J forums see http://www.jsoftware.com/forums.htm
> >>
> > --
> > For information about J forums see http://www.jsoftware.com/forums.htm
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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  wrote:
> 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
> calculator, and get the answer instantly. As an engineer, speed and
> accuracy are more important than going through a complicated manual process
> to get that same answer.
>
> I post lots of these J solutions on Quora to remind folks of that fact.
> It's not that I'm against knowing the manual approach to the solutions.
> It's just that the more tools we have to make hard problems easier, the
> better we can solve even harder problems.
>
> Skip Cave
> Cave Consulting LLC
>
> On Sun, Sep 17, 2017 at 12:28 PM, 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
>> 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: would not be a good idea, since the algorithm used
>> in p: is not known to the audience.  It also seemed like some kind of
>> cheating, like using a subroutine someone else wrote to solve half the
>> problem.
>> Well, I also thought p: would have to generate the array of primes for
>> each call, but it's obviously not the case. There is a formula which does
>> the trick? Or a hash table of primes?
>>
>> thru=: <. + >:@>. i.@- <.
>>
>> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
>>
>> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>
>> s=: 10 f 100
>>
>> s
>>
>> 20 21 22
>>
>> >./90 91 92 93 94 95 96 = s
>>
>> |length error: scriptd
>>
>> | >./90 91 92 93 94 95 96 =s
>>
>> |[-5] j:\j64-803-user\projects\quoraraul.ijs
>>
>> inv
>>
>> ^:_1
>>
>>
>> Cheers,
>>
>> Erling
>>
>> On 2017-09-17 17:38, Raul Miller wrote:
>>
>>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>>
>>> There will be cases where another approach could be more efficient,
>>> but it performs reasonably well.
>>>
>>> Thanks,
>>>
>>>
>> --
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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. 20:34, skrev 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, for arguments larger than 2^31, 1 p: y will 
give an incorrect result?

/Erling

On 2017-09-17 19:28, 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 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: would not be a good idea, since the 
algorithm used in p: is not known to the audience.  It also seemed 
like some kind of cheating, like using a subroutine someone else 
wrote to solve half the problem.
Well, I also thought p: would have to generate the array of primes 
for each call, but it's obviously not the case. There is a formula 
which does the trick? Or a hash table of primes?


thru=: <. + >:@>. i.@- <.

biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]

f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

s=: 10 f 100

s

20 21 22

>./90 91 92 93 94 95 96 = s

|length error: scriptd

| >./90 91 92 93 94 95 96 =s

|[-5] j:\j64-803-user\projects\quoraraul.ijs

inv

^:_1


Cheers,

Erling

On 2017-09-17 17:38, Raul Miller wrote:

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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

There will be cases where another approach could be more efficient,
but it performs reasonably well.

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm



--
For information about J forums see http://www.jsoftware.com/forums.htm


--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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, Sep 17, 2017 at 11:34 AM, Erling Hellenäs 
wrote:


"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, for arguments larger than 2^31, 1 p: y will
give an incorrect result?
/Erling


On 2017-09-17 19:28, 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
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: would not be a good idea, since the algorithm
used in p: is not known to the audience.  It also seemed like some kind of
cheating, like using a subroutine someone else wrote to solve half the
problem.
Well, I also thought p: would have to generate the array of primes for
each call, but it's obviously not the case. There is a formula which does
the trick? Or a hash table of primes?

thru=: <. + >:@>. i.@- <.

biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]

f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

s=: 10 f 100

s

20 21 22


./90 91 92 93 94 95 96 = s

|length error: scriptd

| >./90 91 92 93 94 95 96 =s

|[-5] j:\j64-803-user\projects\quoraraul.ijs

inv

^:_1


Cheers,

Erling

On 2017-09-17 17:38, Raul Miller wrote:


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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

There will be cases where another approach could be more efficient,
but it performs reasonably well.

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm



--
For information about J forums see http://www.jsoftware.com/forums.htm


--
For information about J forums see http://www.jsoftware.com/forums.htm


--
For information about J forums see http://www.jsoftware.com/forums.htm

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 completely in APL http://dfns.dyalog.com/n_pco.htm .  The design is
the same as p: except that there is a new computation for a left argument
of 10, wherein:  If b=. 10 pco m,n then b is a boolean vector of length n-m
and m+I.b are all the primes between m and n (>:m but < n).  That is, b is
a prime sieve.  (Hence the 10 left argument, get it? :-)  Y'all can
implement this extension to the p: in J.

Anyway, with this extension, the Quora Challenge can be met as follows:

pc←{
  p←   4 pco ⍺-1
  q←1+¯4 pco ⍵+1
  j←d⍳⌈/d←-2-/i←⍸10 pco p,q
  (p+i[j]+1)+⍳d[j]-1
}

Translating from APL to J:

p  : the smallest prime >:x
q  : the largest  prime <:y
10 pco p,q : the sieve to select all the primes between p and q
⍸  : APL equivalent of I.
2 -/ i : APL equivalent of 2 -/\ i
d⍳⌈/d  : the index of the maximum value in d
⍳  : APL equivalent of i.
blah[k]: indexing

My previous teaser showed that 2001401814+i.185 is an interval of
composites.  Checking that it is the longest such interval between 1e6+2e9
and 2e6+2e9 is left as an exercise for the reader. :-)



On Sun, Sep 17, 2017 at 2:12 PM, Roger Hui 
wrote:

> > 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 2001401995 2001401996 2001401997 2001401998
>
>   t ≡ (1↑t)+⍳185
> 1
>
>   +/ 1 pco t
> 0
>   ⍝ that is, all composite
>
>   1 pco ( 1↑t)-1
> 1
>   1 pco (¯1↑t)+1
> 1
>   ⍝ that is, the interval is bracketed by primes
>
>   1 1 cmpx '(1e6+2e9) pc 2e6+2e9'
> 0.074125
>
> Takes 0.074 seconds on my machine.
> ​
>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 2001401995 2001401996 2001401997 2001401998

  t ≡ (1↑t)+⍳185
1

  +/ 1 pco t
0
  ⍝ that is, all composite

  1 pco ( 1↑t)-1
1
  1 pco (¯1↑t)+1
1
  ⍝ that is, the interval is bracketed by primes

  1 1 cmpx '(1e6+2e9) pc 2e6+2e9'
0.074125

Takes 0.074 seconds on my machine.
​
--
For information about J forums see http://www.jsoftware.com/forums.htm

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, Sep 17, 2017 at 11:34 AM, Erling Hellenäs 
wrote:

> "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, for arguments larger than 2^31, 1 p: y will
> give an incorrect result?
> /Erling
>
>
> On 2017-09-17 19:28, 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
>> 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: would not be a good idea, since the algorithm
>> used in p: is not known to the audience.  It also seemed like some kind of
>> cheating, like using a subroutine someone else wrote to solve half the
>> problem.
>> Well, I also thought p: would have to generate the array of primes for
>> each call, but it's obviously not the case. There is a formula which does
>> the trick? Or a hash table of primes?
>>
>> thru=: <. + >:@>. i.@- <.
>>
>> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
>>
>> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>
>> s=: 10 f 100
>>
>> s
>>
>> 20 21 22
>>
>> >./90 91 92 93 94 95 96 = s
>>
>> |length error: scriptd
>>
>> | >./90 91 92 93 94 95 96 =s
>>
>> |[-5] j:\j64-803-user\projects\quoraraul.ijs
>>
>> inv
>>
>> ^:_1
>>
>>
>> Cheers,
>>
>> Erling
>>
>> On 2017-09-17 17:38, Raul Miller wrote:
>>
>>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>>
>>> There will be cases where another approach could be more efficient,
>>> but it performs reasonably well.
>>>
>>> Thanks,
>>>
>>>
>> --
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>
>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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, for arguments larger than 2^31, 1 p: y will 
give an incorrect result?

/Erling

On 2017-09-17 19:28, 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 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: would not be a good idea, since the 
algorithm used in p: is not known to the audience.  It also seemed 
like some kind of cheating, like using a subroutine someone else wrote 
to solve half the problem.
Well, I also thought p: would have to generate the array of primes for 
each call, but it's obviously not the case. There is a formula which 
does the trick? Or a hash table of primes?


thru=: <. + >:@>. i.@- <.

biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]

f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

s=: 10 f 100

s

20 21 22

>./90 91 92 93 94 95 96 = s

|length error: scriptd

| >./90 91 92 93 94 95 96 =s

|[-5] j:\j64-803-user\projects\quoraraul.ijs

inv

^:_1


Cheers,

Erling

On 2017-09-17 17:38, Raul Miller wrote:

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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

There will be cases where another approach could be more efficient,
but it performs reasonably well.

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm



--
For information about J forums see http://www.jsoftware.com/forums.htm

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

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s

s=: 2000 f 3000

s

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s

timespacex'2 f 200'

1.36076e_5 4736

timespacex'2 f 2000'

2.53595e_5 22656

timespacex'2 f 2'

9.46343e_5 166016

timespacex'2 f 20'

0.00184228 1.3129e6

timespacex'2 f 200'

0.0159511 1.04882e7

timespacex'2 f 2000'

0.133137 8.38885e7



On 2017-09-17 19:58, Raul Miller wrote:

Oops,

biggap=:{~0 1+[:(i.<./)2-/\]


Thanks,




--
For information about J forums see http://www.jsoftware.com/forums.htm

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
calculator, and get the answer instantly. As an engineer, speed and
accuracy are more important than going through a complicated manual process
to get that same answer.

I post lots of these J solutions on Quora to remind folks of that fact.
It's not that I'm against knowing the manual approach to the solutions.
It's just that the more tools we have to make hard problems easier, the
better we can solve even harder problems.

Skip Cave
Cave Consulting LLC

On Sun, Sep 17, 2017 at 12:28 PM, 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
> 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: would not be a good idea, since the algorithm used
> in p: is not known to the audience.  It also seemed like some kind of
> cheating, like using a subroutine someone else wrote to solve half the
> problem.
> Well, I also thought p: would have to generate the array of primes for
> each call, but it's obviously not the case. There is a formula which does
> the trick? Or a hash table of primes?
>
> thru=: <. + >:@>. i.@- <.
>
> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
>
> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>
> s=: 10 f 100
>
> s
>
> 20 21 22
>
> >./90 91 92 93 94 95 96 = s
>
> |length error: scriptd
>
> | >./90 91 92 93 94 95 96 =s
>
> |[-5] j:\j64-803-user\projects\quoraraul.ijs
>
> inv
>
> ^:_1
>
>
> Cheers,
>
> Erling
>
> On 2017-09-17 17:38, Raul Miller wrote:
>
>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>
>> There will be cases where another approach could be more efficient,
>> but it performs reasonably well.
>>
>> Thanks,
>>
>>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
For information about J forums see http://www.jsoftware.com/forums.htm

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
> 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: would not be a good idea, since the algorithm used
> in p: is not known to the audience.  It also seemed like some kind of
> cheating, like using a subroutine someone else wrote to solve half the
> problem.
> Well, I also thought p: would have to generate the array of primes for
> each call, but it's obviously not the case. There is a formula which does
> the trick? Or a hash table of primes?
>
> thru=: <. + >:@>. i.@- <.
>
> biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
>
> f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>
> s=: 10 f 100
>
> s
>
> 20 21 22
>
> >./90 91 92 93 94 95 96 = s
>
> |length error: scriptd
>
> | >./90 91 92 93 94 95 96 =s
>
> |[-5] j:\j64-803-user\projects\quoraraul.ijs
>
> inv
>
> ^:_1
>
>
> Cheers,
>
> Erling
>
> On 2017-09-17 17:38, Raul Miller wrote:
>
>> 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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
>>
>> There will be cases where another approach could be more efficient,
>> but it performs reasonably well.
>>
>> Thanks,
>>
>>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 information about J forums see http://www.jsoftware.com/forums.htm

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: would not be a good idea, since the 
algorithm used in p: is not known to the audience.  It also seemed like 
some kind of cheating, like using a subroutine someone else wrote to 
solve half the problem.
Well, I also thought p: would have to generate the array of primes for 
each call, but it's obviously not the case. There is a formula which 
does the trick? Or a hash table of primes?


thru=: <. + >:@>. i.@- <.

biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]

f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

s=: 10 f 100

s

20 21 22

>./90 91 92 93 94 95 96 = s

|length error: scriptd

| >./90 91 92 93 94 95 96 =s

|[-5] j:\j64-803-user\projects\quoraraul.ijs

inv

^:_1


Cheers,

Erling

On 2017-09-17 17:38, Raul Miller wrote:

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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

There will be cases where another approach could be more efficient,
but it performs reasonably well.

Thanks,



--
For information about J forums see http://www.jsoftware.com/forums.htm

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=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)

There will be cases where another approach could be more efficient,
but it performs reasonably well.

Thanks,

-- 
Raul

On Sun, Sep 17, 2017 at 11:04 AM, Erling Hellenäs
 wrote:
> 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 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 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 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
>>>
>>> primes=.i.0
>>>
>>> n=. 2 + i. y - 1
>>>
>>> for_i. n do.
>>>
>>> primes=.primes,(primes Prime i) # i
>>>
>>> end.
>>>
>>> primes
>>>
>>> )
>>>
>>> PrimesUntil 5
>>>
>>> 2 3 5
>>>
>>> NB. 2 3 5
>>>
>>> PrimesUntil 10
>>>
>>> 2 3 5 7
>>>
>>> NB. 2 3 5 7
>>>
>>> PrimesUntil 11
>>>
>>> 2 3 5 7 11
>>>
>>> NB. 2 3 5 7 11
>>>
>>> PrimesInRange=: 4 : 0
>>>
>>> n=. PrimesUntil y
>>>
>>> (n >: x)# n
>>>
>>> )
>>>
>>> 7 PrimesInRange 11
>>>
>>> 7 11
>>>
>>> NB. 7 11
>>>
>>> 7 PrimesInRange 10
>>>
>>> 7
>>>
>>> NB. 7
>>>
>>> 8 PrimesInRange 10
>>>
>>>
>>> NB. i.0
>>>
>>> AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]
>>>
>>> 3 AddStartRangeIfNotThere 3 5 7 11
>>>
>>> 3 5 7 11
>>>
>>> NB. 3 5 7 11
>>>
>>> 8 AddStartRangeIfNotThere i.0
>>>
>>> 8
>>>
>>> NB. 8
>>>
>>> AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [
>>>
>>> 11 AddEndRangeIfNotThere 3 5 7 11
>>>
>>> 3 5 7 11
>>>
>>> NB. 3 5 7 11
>>>
>>> 10 AddEndRangeIfNotThere 8
>>>
>>> 8 10
>>>
>>> NB. 8 10
>>>
>>> SequenceLengths=: 1 + (1 }. ]) - _1 }. ]
>>>
>>> SequenceLengths 3 5 7 11
>>>
>>> 3 3 5
>>>
>>> NB.3 3 5
>>>
>>> SequenceLengths 8 10
>>>
>>> 3
>>>
>>> NB. 3
>>>
>>> FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]
>>>
>>> 3 3 5 FirstInLongestSequence 3 5 7 11
>>>
>>> 7
>>>
>>> NB. 7
>>>
>>> 3 FirstInLongestSequence 8 10
>>>
>>> 8
>>>
>>> NB. 8
>>>
>>> SequenceIndices=: ] + [: i. [: >./ [
>>>
>>> 3 3 5 SequenceIndices 7
>>>
>>> 7 8 9 10 11
>>>
>>> NB. 7 8 9 10 11
>>>
>>> (,3) SequenceIndices 8
>>>
>>> 8 9 10
>>>
>>> NB. 8 9 10
>>>
>>> DropFirstIfPrime=: ((1 {. ]) e. [) }. ]
>>>
>>> 7 11 DropFirstIfPrime 7 8 9 10 11
>>>
>>> 8 9 10 11
>>>
>>> NB. 8 9 10 11
>>>
>>> (i.0) DropFirstIfPrime 8 9 10
>>>
>>> 8 9 10
>>>
>>> NB. 8 9 10
>>>
>>> DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]
>>>
>>> (,11)DropLastIfPrime 8 9 10 11
>>>
>>> 8 9 10
>>>
>>> NB. 8 9 10
>>>
>>> (i.0)DropLastIfPrime 8 9 10
>>>
>>> 8 9 10
>>>
>>> f=: 4 : 0
>>>
>>> NB. Find the longest sequence of non-primes in a range
>>>
>>> NB. x and y are the range limits
>>>
>>> primesInRange =. x PrimesInRange y
>>>
>>> NB. Add range limits if they are not primes and then already there
>>>
>>> rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere
>>> primesInRange
>>>
>>> NB. The lengths of the possible sequences, including both sequence limits
>>>
>>> sequenceLengths =. SequenceLengths rangeLimits
>>>
>>> NB. The first element of the longest sequence
>>>
>>> firstInLongestSequence=.sequenceLengths FirstInLongestSequence
>>> rangeLimits
>>>
>>> NB. The longest range including it's both limits
>>>
>>> longestRange =. sequenceLengths SequenceIndices firstInLongestSequence
>>>
>>> NB. Drop the range limits if they are primes
>>>
>>> primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange
>>>
>>> )
>>>
>>> s=: 10 f 100
>>>
>>> s
>>>
>>> 90 91 92 93 94 95 96
>>>
>>> >./90 91 92 93 94 95 96 = s
>>>
>>> 1
>>>
>>> s=: 1 f 2
>>>
>>> >./s = 1
>>>
>>> 1
>>>
>>> s=: 2 f 3
>>>
>>> 0 = $ s
>>>
>>> 1
>>>
>>> s=: 8 f 9
>>>
>>> >./s = 8 9
>>>
>>> 1
>>>
>>> s=: 7 f 8
>>>
>>> >./s = 8
>>>
>>> 1
>>>
>>> s=: 10 f 11
>>>
>>> >./s = 10
>>>
>>> 1
>>>
>>> s=: 7 f 11
>>>
>>> >./s = 8 9 10
>>>
>>> 1
>>>
>>> s=: 200 f 300
>>>
>>> s
>>>
>>> 212 213 214 215 216 217 218 219 220 221 222
>>>
>>> >./0 = 1 p: s
>>>
>>> 1
>>>
>>> 1 = 1 p: _1 + 1 {. s
>>>
>>> 1
>>>
>>> 1 = 1 p: 1 + _1 {. s
>>>
>>> 1
>>>
>>> s=: 2000 

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 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
>
> primes=.i.0
>
> n=. 2 + i. y - 1
>
> for_i. n do.
>
> primes=.primes,(primes Prime i) # i
>
> end.
>
> primes
>
> )
>
> PrimesUntil 5
>
> 2 3 5
>
> NB. 2 3 5
>
> PrimesUntil 10
>
> 2 3 5 7
>
> NB. 2 3 5 7
>
> PrimesUntil 11
>
> 2 3 5 7 11
>
> NB. 2 3 5 7 11
>
> PrimesInRange=: 4 : 0
>
> n=. PrimesUntil y
>
> (n >: x)# n
>
> )
>
> 7 PrimesInRange 11
>
> 7 11
>
> NB. 7 11
>
> 7 PrimesInRange 10
>
> 7
>
> NB. 7
>
> 8 PrimesInRange 10
>
>
> NB. i.0
>
> AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]
>
> 3 AddStartRangeIfNotThere 3 5 7 11
>
> 3 5 7 11
>
> NB. 3 5 7 11
>
> 8 AddStartRangeIfNotThere i.0
>
> 8
>
> NB. 8
>
> AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [
>
> 11 AddEndRangeIfNotThere 3 5 7 11
>
> 3 5 7 11
>
> NB. 3 5 7 11
>
> 10 AddEndRangeIfNotThere 8
>
> 8 10
>
> NB. 8 10
>
> SequenceLengths=: 1 + (1 }. ]) - _1 }. ]
>
> SequenceLengths 3 5 7 11
>
> 3 3 5
>
> NB.3 3 5
>
> SequenceLengths 8 10
>
> 3
>
> NB. 3
>
> FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]
>
> 3 3 5 FirstInLongestSequence 3 5 7 11
>
> 7
>
> NB. 7
>
> 3 FirstInLongestSequence 8 10
>
> 8
>
> NB. 8
>
> SequenceIndices=: ] + [: i. [: >./ [
>
> 3 3 5 SequenceIndices 7
>
> 7 8 9 10 11
>
> NB. 7 8 9 10 11
>
> (,3) SequenceIndices 8
>
> 8 9 10
>
> NB. 8 9 10
>
> DropFirstIfPrime=: ((1 {. ]) e. [) }. ]
>
> 7 11 DropFirstIfPrime 7 8 9 10 11
>
> 8 9 10 11
>
> NB. 8 9 10 11
>
> (i.0) DropFirstIfPrime 8 9 10
>
> 8 9 10
>
> NB. 8 9 10
>
> DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]
>
> (,11)DropLastIfPrime 8 9 10 11
>
> 8 9 10
>
> NB. 8 9 10
>
> (i.0)DropLastIfPrime 8 9 10
>
> 8 9 10
>
> f=: 4 : 0
>
> NB. Find the longest sequence of non-primes in a range
>
> NB. x and y are the range limits
>
> primesInRange =. x PrimesInRange y
>
> NB. Add range limits if they are not primes and then already there
>
> rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere
> primesInRange
>
> NB. The lengths of the possible sequences, including both sequence limits
>
> sequenceLengths =. SequenceLengths rangeLimits
>
> NB. The first element of the longest sequence
>
> firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits
>
> NB. The longest range including it's both limits
>
> longestRange =. sequenceLengths SequenceIndices firstInLongestSequence
>
> NB. Drop the range limits if they are primes
>
> primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange
>
> )
>
> s=: 10 f 100
>
> s
>
> 90 91 92 93 94 95 96
>
>>./90 91 92 93 94 95 96 = s
>
> 1
>
> s=: 1 f 2
>
>>./s = 1
>
> 1
>
> s=: 2 f 3
>
> 0 = $ s
>
> 1
>
> s=: 8 f 9
>
>>./s = 8 9
>
> 1
>
> s=: 7 f 8
>
>>./s = 8
>
> 1
>
> s=: 10 f 11
>
>>./s = 10
>
> 1
>
> s=: 7 f 11
>
>>./s = 8 9 10
>
> 1
>
> s=: 200 f 300
>
> s
>
> 212 213 214 215 216 217 218 219 220 221 222
>
>>./0 = 1 p: s
>
> 1
>
> 1 = 1 p: _1 + 1 {. s
>
> 1
>
> 1 = 1 p: 1 + _1 {. s
>
> 1
>
> s=: 2000 f 3000
>
> s
>
> 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
> 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
>
>>./0 = 1 p: s
>
> 1
>
> 1 = 1 p: _1 + 1 {. s
>
> 1
>
> 1 = 1 p: 1 + _1 {. s
>
> 1
>
> timespacex'200 f 300'
>
> 0.00132426 18816
>
>
> On 2017-09-17 15:35, Raul Miller wrote:
>>
>> 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,
>>
>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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 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 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

primes=.i.0

n=. 2 + i. y - 1

for_i. n do.

primes=.primes,(primes Prime i) # i

end.

primes

)

PrimesUntil 5

2 3 5

NB. 2 3 5

PrimesUntil 10

2 3 5 7

NB. 2 3 5 7

PrimesUntil 11

2 3 5 7 11

NB. 2 3 5 7 11

PrimesInRange=: 4 : 0

n=. PrimesUntil y

(n >: x)# n

)

7 PrimesInRange 11

7 11

NB. 7 11

7 PrimesInRange 10

7

NB. 7

8 PrimesInRange 10


NB. i.0

AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]

3 AddStartRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

8 AddStartRangeIfNotThere i.0

8

NB. 8

AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [

11 AddEndRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

10 AddEndRangeIfNotThere 8

8 10

NB. 8 10

SequenceLengths=: 1 + (1 }. ]) - _1 }. ]

SequenceLengths 3 5 7 11

3 3 5

NB.3 3 5

SequenceLengths 8 10

3

NB. 3

FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]

3 3 5 FirstInLongestSequence 3 5 7 11

7

NB. 7

3 FirstInLongestSequence 8 10

8

NB. 8

SequenceIndices=: ] + [: i. [: >./ [

3 3 5 SequenceIndices 7

7 8 9 10 11

NB. 7 8 9 10 11

(,3) SequenceIndices 8

8 9 10

NB. 8 9 10

DropFirstIfPrime=: ((1 {. ]) e. [) }. ]

7 11 DropFirstIfPrime 7 8 9 10 11

8 9 10 11

NB. 8 9 10 11

(i.0) DropFirstIfPrime 8 9 10

8 9 10

NB. 8 9 10

DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]

(,11)DropLastIfPrime 8 9 10 11

8 9 10

NB. 8 9 10

(i.0)DropLastIfPrime 8 9 10

8 9 10

f=: 4 : 0

NB. Find the longest sequence of non-primes in a range

NB. x and y are the range limits

primesInRange =. x PrimesInRange y

NB. Add range limits if they are not primes and then already there

rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere 
primesInRange


NB. The lengths of the possible sequences, including both sequence limits

sequenceLengths =. SequenceLengths rangeLimits

NB. The first element of the longest sequence

firstInLongestSequence=.sequenceLengths FirstInLongestSequence 
rangeLimits


NB. The longest range including it's both limits

longestRange =. sequenceLengths SequenceIndices firstInLongestSequence

NB. Drop the range limits if they are primes

primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange

)

s=: 10 f 100

s

90 91 92 93 94 95 96

>./90 91 92 93 94 95 96 = s

1

s=: 1 f 2

>./s = 1

1

s=: 2 f 3

0 = $ s

1

s=: 8 f 9

>./s = 8 9

1

s=: 7 f 8

>./s = 8

1

s=: 10 f 11

>./s = 10

1

s=: 7 f 11

>./s = 8 9 10

1

s=: 200 f 300

s

212 213 214 215 216 217 218 219 220 221 222

>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

s=: 2000 f 3000

s

2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998


>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

timespacex'200 f 300'

0.00132426 18816


On 2017-09-17 15:35, Raul Miller wrote:

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,







--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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 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

primes=.i.0

n=. 2 + i. y - 1

for_i. n do.

primes=.primes,(primes Prime i) # i

end.

primes

)

PrimesUntil 5

2 3 5

NB. 2 3 5

PrimesUntil 10

2 3 5 7

NB. 2 3 5 7

PrimesUntil 11

2 3 5 7 11

NB. 2 3 5 7 11

PrimesInRange=: 4 : 0

n=. PrimesUntil y

(n >: x)# n

)

7 PrimesInRange 11

7 11

NB. 7 11

7 PrimesInRange 10

7

NB. 7

8 PrimesInRange 10


NB. i.0

AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]

3 AddStartRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

8 AddStartRangeIfNotThere i.0

8

NB. 8

AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [

11 AddEndRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

10 AddEndRangeIfNotThere 8

8 10

NB. 8 10

SequenceLengths=: 1 + (1 }. ]) - _1 }. ]

SequenceLengths 3 5 7 11

3 3 5

NB.3 3 5

SequenceLengths 8 10

3

NB. 3

FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]

3 3 5 FirstInLongestSequence 3 5 7 11

7

NB. 7

3 FirstInLongestSequence 8 10

8

NB. 8

SequenceIndices=: ] + [: i. [: >./ [

3 3 5 SequenceIndices 7

7 8 9 10 11

NB. 7 8 9 10 11

(,3) SequenceIndices 8

8 9 10

NB. 8 9 10

DropFirstIfPrime=: ((1 {. ]) e. [) }. ]

7 11 DropFirstIfPrime 7 8 9 10 11

8 9 10 11

NB. 8 9 10 11

(i.0) DropFirstIfPrime 8 9 10

8 9 10

NB. 8 9 10

DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]

(,11)DropLastIfPrime 8 9 10 11

8 9 10

NB. 8 9 10

(i.0)DropLastIfPrime 8 9 10

8 9 10

f=: 4 : 0

NB. Find the longest sequence of non-primes in a range

NB. x and y are the range limits

primesInRange =. x PrimesInRange y

NB. Add range limits if they are not primes and then already there

rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere 
primesInRange


NB. The lengths of the possible sequences, including both sequence limits

sequenceLengths =. SequenceLengths rangeLimits

NB. The first element of the longest sequence

firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits

NB. The longest range including it's both limits

longestRange =. sequenceLengths SequenceIndices firstInLongestSequence

NB. Drop the range limits if they are primes

primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange

)

s=: 10 f 100

s

90 91 92 93 94 95 96

>./90 91 92 93 94 95 96 = s

1

s=: 1 f 2

>./s = 1

1

s=: 2 f 3

0 = $ s

1

s=: 8 f 9

>./s = 8 9

1

s=: 7 f 8

>./s = 8

1

s=: 10 f 11

>./s = 10

1

s=: 7 f 11

>./s = 8 9 10

1

s=: 200 f 300

s

212 213 214 215 216 217 218 219 220 221 222

>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

s=: 2000 f 3000

s

2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998


>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

timespacex'200 f 300'

0.00132426 18816


On 2017-09-17 15:35, Raul Miller wrote:

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,





--
For information about J forums see http://www.jsoftware.com/forums.htm

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

primes=.i.0

n=. 2 + i. y - 1

for_i. n do.

primes=.primes,(primes Prime i) # i

end.

primes

)

PrimesUntil 5

2 3 5

NB. 2 3 5

PrimesUntil 10

2 3 5 7

NB. 2 3 5 7

PrimesUntil 11

2 3 5 7 11

NB. 2 3 5 7 11

PrimesInRange=: 4 : 0

n=. PrimesUntil y

(n >: x)# n

)

7 PrimesInRange 11

7 11

NB. 7 11

7 PrimesInRange 10

7

NB. 7

8 PrimesInRange 10


NB. i.0

AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]

3 AddStartRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

8 AddStartRangeIfNotThere i.0

8

NB. 8

AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [

11 AddEndRangeIfNotThere 3 5 7 11

3 5 7 11

NB. 3 5 7 11

10 AddEndRangeIfNotThere 8

8 10

NB. 8 10

SequenceLengths=: 1 + (1 }. ]) - _1 }. ]

SequenceLengths 3 5 7 11

3 3 5

NB.3 3 5

SequenceLengths 8 10

3

NB. 3

FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]

3 3 5 FirstInLongestSequence 3 5 7 11

7

NB. 7

3 FirstInLongestSequence 8 10

8

NB. 8

SequenceIndices=: ] + [: i. [: >./ [

3 3 5 SequenceIndices 7

7 8 9 10 11

NB. 7 8 9 10 11

(,3) SequenceIndices 8

8 9 10

NB. 8 9 10

DropFirstIfPrime=: ((1 {. ]) e. [) }. ]

7 11 DropFirstIfPrime 7 8 9 10 11

8 9 10 11

NB. 8 9 10 11

(i.0) DropFirstIfPrime 8 9 10

8 9 10

NB. 8 9 10

DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]

(,11)DropLastIfPrime 8 9 10 11

8 9 10

NB. 8 9 10

(i.0)DropLastIfPrime 8 9 10

8 9 10

f=: 4 : 0

NB. Find the longest sequence of non-primes in a range

NB. x and y are the range limits

primesInRange =. x PrimesInRange y

NB. Add range limits if they are not primes and then already there

rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere 
primesInRange


NB. The lengths of the possible sequences, including both sequence limits

sequenceLengths =. SequenceLengths rangeLimits

NB. The first element of the longest sequence

firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits

NB. The longest range including it's both limits

longestRange =. sequenceLengths SequenceIndices firstInLongestSequence

NB. Drop the range limits if they are primes

primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange

)

s=: 10 f 100

s

90 91 92 93 94 95 96

>./90 91 92 93 94 95 96 = s

1

s=: 1 f 2

>./s = 1

1

s=: 2 f 3

0 = $ s

1

s=: 8 f 9

>./s = 8 9

1

s=: 7 f 8

>./s = 8

1

s=: 10 f 11

>./s = 10

1

s=: 7 f 11

>./s = 8 9 10

1

s=: 200 f 300

s

212 213 214 215 216 217 218 219 220 221 222

>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

s=: 2000 f 3000

s

2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998


>./0 = 1 p: s

1

1 = 1 p: _1 + 1 {. s

1

1 = 1 p: 1 + _1 {. s

1

timespacex'200 f 300'

0.00132426 18816


On 2017-09-17 15:35, Raul Miller wrote:

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,



--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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
>
> NB. 0
>
> PrimesUntil=: 3 : 0
>
> primes=.i.0
>
> n=. 2 + i. y - 1
>
> for_i. n do.
>
> primes=.primes,(primes Prime i) # i
>
> end.
>
> primes
>
> )
>
> PrimesUntil 5
>
> NB. 2 3 5
>
> PrimesUntil 10
>
> NB. 2 3 5 7
>
> PrimesUntil 11
>
> NB. 2 3 5 7 11
>
> PrimesInRange=: 4 : 0
>
> n=. PrimesUntil y
>
> (n >: x)# n
>
> )
>
> 7 PrimesInRange 11
>
> NB. 7 11
>
> 7 PrimesInRange 10
>
> NB. 7
>
> 8 PrimesInRange 10
>
>
> NB. i.0
>
> AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]
>
> 3 AddStartRangeIfNotThere 3 5 7 11
>
> NB. 3 5 7 11
>
> 8 AddStartRangeIfNotThere i.0
>
> NB. 8
>
> AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [
>
> 11 AddEndRangeIfNotThere 3 5 7 11
>
> NB. 3 5 7 11
>
> 10 AddEndRangeIfNotThere 8
>
> NB. 8 10
>
> SequenceLengths=: 1 + (1 }. ]) - _1 }. ]
>
> SequenceLengths 3 5 7 11
>
> NB.3 3 5
>
> SequenceLengths 8 10
>
> NB. 3
>
> FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]
>
> 3 3 5 FirstInLongestSequence 3 5 7 11
>
> NB. 7
>
> 3 FirstInLongestSequence 8 10
>
> NB. 8
>
> SequenceIndices=: ] + [: i. [: >./ [
>
> 3 3 5 SequenceIndices 7
>
> NB. 7 8 9 10 11
>
> (,3) SequenceIndices 8
>
> NB. 8 9 10
>
> DropFirstIfPrime=: ((1 {. ]) e. [) }. ]
>
> 7 11 DropFirstIfPrime 7 8 9 10 11
>
> NB. 8 9 10 11
>
> (i.0) DropFirstIfPrime 8 9 10
>
> NB. 8 9 10
>
> DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]
>
> (,11)DropLastIfPrime 8 9 10 11
>
> NB. 8 9 10
>
> (i.0)DropLastIfPrime 8 9 10
>
> f=: 4 : 0
>
> NB. Find the longest sequence of non-primes in a range
>
> NB. x and y are the range limits
>
> primesInRange =. x PrimesInRange y
>
> NB. Add range limits if they are not primes and then already there
>
> rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere
> primesInRange
>
> NB. The lengths of the possible sequences, including both sequence limits
>
> sequenceLengths =. SequenceLengths rangeLimits
>
> NB. The first element of the longest sequence
>
> firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits
>
> NB. The longest range including it's both limits
>
> longestRange =. sequenceLengths SequenceIndices firstInLongestSequence
>
> NB. Drop the range limits if they are primes
>
> primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange
>
> )
>
>
> s=: 10 f 100
>
> s
>
>>./90 91 92 93 94 95 96 = s
>
> s=: 1 f 2
>
>>./s = 1
>
> s=: 2 f 3
>
> 0 = $ s
>
> s=: 8 f 9
>
>>./s = 8 9
>
> s=: 7 f 8
>
>>./s = 8
>
> s=: 10 f 11
>
>>./s = 10
>
> s=: 7 f 11
>
>>./s = 8 9 10
>
> s=: 200 f 300
>
> s
>
>>./0 = 1 p: s
>
> 1 = 1 p: _1 + 1 {. s
>
> 1 = 1 p: 1 + _1 {. s
>
> s=: 2000 f 3000
>
> s
>
>>./0 = 1 p: s
>
> 1 = 1 p: _1 + 1 {. s
>
> 1 = 1 p: 1 + _1 {. s
>
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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

NB. 0

PrimesUntil=: 3 : 0

primes=.i.0

n=. 2 + i. y - 1

for_i. n do.

primes=.primes,(primes Prime i) # i

end.

primes

)

PrimesUntil 5

NB. 2 3 5

PrimesUntil 10

NB. 2 3 5 7

PrimesUntil 11

NB. 2 3 5 7 11

PrimesInRange=: 4 : 0

n=. PrimesUntil y

(n >: x)# n

)

7 PrimesInRange 11

NB. 7 11

7 PrimesInRange 10

NB. 7

8 PrimesInRange 10

NB. i.0

AddStartRangeIfNotThere=: (([ ~: 1 {. ]) # [) , ]

3 AddStartRangeIfNotThere 3 5 7 11

NB. 3 5 7 11

8 AddStartRangeIfNotThere i.0

NB. 8

AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [

11 AddEndRangeIfNotThere 3 5 7 11

NB. 3 5 7 11

10 AddEndRangeIfNotThere 8

NB. 8 10

SequenceLengths=: 1 + (1 }. ]) - _1 }. ]

SequenceLengths 3 5 7 11

NB.3 3 5

SequenceLengths 8 10

NB. 3

FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]

3 3 5 FirstInLongestSequence 3 5 7 11

NB. 7

3 FirstInLongestSequence 8 10

NB. 8

SequenceIndices=: ] + [: i. [: >./ [

3 3 5 SequenceIndices 7

NB. 7 8 9 10 11

(,3) SequenceIndices 8

NB. 8 9 10

DropFirstIfPrime=: ((1 {. ]) e. [) }. ]

7 11 DropFirstIfPrime 7 8 9 10 11

NB. 8 9 10 11

(i.0) DropFirstIfPrime 8 9 10

NB. 8 9 10

DropLastIfPrime=: ([: -(_1 {. ]) e. [) }. ]

(,11)DropLastIfPrime 8 9 10 11

NB. 8 9 10

(i.0)DropLastIfPrime 8 9 10

f=: 4 : 0

NB. Find the longest sequence of non-primes in a range

NB. x and y are the range limits

primesInRange =. x PrimesInRange y

NB. Add range limits if they are not primes and then already there

rangeLimits =. y AddEndRangeIfNotThere x AddStartRangeIfNotThere 
primesInRange


NB. The lengths of the possible sequences, including both sequence limits

sequenceLengths =. SequenceLengths rangeLimits

NB. The first element of the longest sequence

firstInLongestSequence=.sequenceLengths FirstInLongestSequence rangeLimits

NB. The longest range including it's both limits

longestRange =. sequenceLengths SequenceIndices firstInLongestSequence

NB. Drop the range limits if they are primes

primesInRange DropLastIfPrime primesInRange DropFirstIfPrime longestRange

)

s=: 10 f 100

s

>./90 91 92 93 94 95 96 = s

s=: 1 f 2

>./s = 1

s=: 2 f 3

0 = $ s

s=: 8 f 9

>./s = 8 9

s=: 7 f 8

>./s = 8

s=: 10 f 11

>./s = 10

s=: 7 f 11

>./s = 8 9 10

s=: 200 f 300

s

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s

s=: 2000 f 3000

s

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s

--
For information about J forums see http://www.jsoftware.com/forums.htm

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 {. ]) # [) , ]

3 AddStartRangeIfNotThere 3 5 7 11

NB. 3 5 7 11

8 AddStartRangeIfNotThere i.0

NB. 8

AddEndRangeIfNotThere=: ] , ([ ~: _1 {. ]) # [

11 AddEndRangeIfNotThere 3 5 7 11

NB. 3 5 7 11

10 AddEndRangeIfNotThere 8

NB. 8 10

SequenceLengths=: 1 + (1 }. ]) - _1 }. ]

SequenceLengths 3 5 7 11

NB.3 3 5

SequenceLengths 8 10

NB. 3

FirstInLongestSequence=: '' $ (([ = [: >./ [) , 0:) # ]

3 3 5 FirstInLongestSequence 3 5 7 11

NB. 7

3 FirstInLongestSequence 8 10

NB. 8

SequenceIndices=: ] + [: i. [: >./ [

3 3 5 SequenceIndices 7

NB. 7 8 9 10 11

(,3) SequenceIndices 8

NB. 8 9 10

DropFirstIfPrime=: (1 p: 1 {. ]) }. ]

DropFirstIfPrime 7 8 9 10 11

NB. 8 9 10 11

DropFirstIfPrime 8 9 10

NB. 8 9 10

DropLastIfPrime=: (_1 * 1 p: _1 {. ]) }. ]

DropLastIfPrime 8 9 10 11

NB. 8 9 10

DropLastIfPrime 8 9 10

NB. 8 9 10

f=:[: DropLastIfPrime [: DropFirstIfPrime [: ( SequenceLengths ([ 
SequenceIndices FirstInLongestSequence) ]) ] AddEndRangeIfNotThere [ 
AddStartRangeIfNotThere [: Primes [ RangeIndex ]


s=: 10 f 100

s

>./90 91 92 93 94 95 96 = s

s=: 1 f 2

>./s = 1

s=: 2 f 3

0 = $ s

s=: 8 f 9

>./s = 8 9

s=: 7 f 8

>./s = 8

s=: 10 f 11

>./s = 10

s=: 7 f 11

>./s = 8 9 10

s=: 200 f 300

s

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s

s=: 2000 f 3000

s

>./0 = 1 p: s

1 = 1 p: _1 + 1 {. s

1 = 1 p: 1 + _1 {. s




On 2017-09-15 11:36, Skip Cave wrote:

Write a function f, which will list the longest set of consecutive
non-prime integers between two prime numbers in the interval x, y

for example:

10 f 100
90 91 92 93 94 95 96


Skip Cave
Cave Consulting LLC
--
For information about J forums seehttp://www.jsoftware.com/forums.htm



--
For information about J forums see http://www.jsoftware.com/forums.htm

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 <./ >. >./ <. 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 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 Miller  wrote:
>>> 
>>> 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 consecutive
 non-prime integers between two prime numbers in the interval x, y
 
 for example:
 
   10 f 100
 90 91 92 93 94 95 96
 
 
 Skip Cave
 Cave Consulting LLC
 --
 For information about J forums see http://www.jsoftware.com/forums.htm
>>> --
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>> --
>> For information about J forums see http://www.jsoftware.com/forums.htm
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 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 Miller  wrote:
>
>> 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 consecutive
>> > non-prime integers between two prime numbers in the interval x, y
>> >
>> > for example:
>> >
>> >10 f 100
>> > 90 91 92 93 94 95 96
>> >
>> >
>> > Skip Cave
>> > Cave Consulting LLC
>> > --
>> > For information about J forums see http://www.jsoftware.com/forums.htm
>> --
>> For information about J forums see http://www.jsoftware.com/forums.htm
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 Miller  wrote:

> 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 consecutive
> > non-prime integers between two prime numbers in the interval x, y
> >
> > for example:
> >
> >10 f 100
> > 90 91 92 93 94 95 96
> >
> >
> > Skip Cave
> > Cave Consulting LLC
> > --
> > For information about J forums see http://www.jsoftware.com/forums.htm
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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
> non-prime integers between two prime numbers in the interval x, y
>
> for example:
>
>10 f 100
> 90 91 92 93 94 95 96
>
>
> Skip Cave
> Cave Consulting LLC
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm

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 consecutive
> non-prime integers between two prime numbers in the interval x, y
>
> for example:
>
>10 f 100
> 90 91 92 93 94 95 96
>
>
> Skip Cave
> Cave Consulting LLC
> --
> For information about J forums see http://www.jsoftware.com/forums.htm
--
For information about J forums see http://www.jsoftware.com/forums.htm