Sometimes it helps to start with a smaller example:
(func >:i.15)i. >./func >:i.15
5
(func >:i.1000000)i. >./func >:i.1000000
510509
I would guess this is correct, but don't know a real test for it.
Linda
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jon Hough
Sent: Thursday, June 12, 2014 1:37 AM
To: [email protected]
Subject: [Jprogramming] Maximum totient ratio
Another Project Euler...
This time PE #69: http://projecteuler.net/problem=69
Essentially, find n which has the maximum of n/totient(n), for all positive
integers, n, up to 1000000.
At first this seemed easy. I have a verb to calculate the ratio:
totient=: (- ~:)&.q:
func =. ] % totient
then I can find the maximum:
max=. (>./)"1@:((] % totient)"0)
max 1+i.1000000
5.53939
But this gives me the ratio. I need to find n.
This is a problem, because finding the maximum, the verb, >./ , collapsed
the array so I don't think I can use I. or similar verb to get the index
which gave the maximum ratio. So I am not sure how to find the n which gave
this ratio.
A second issue is I think my method seems to not be well thought out
memory-wise. My method first calculates the ratio for every integer up to 1
million and holds them all in memory. Surely a better way would be to do
them one at a time and find the maximum as we go.
In J I am not sure how to do this. I am guessing the recursion verb $: will
need to be used.
Any help appreciated.
Regards.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm