UpCount

Discussion of challenges you have already solved
theStack
Posts: 72
Joined: Sun Nov 02, 2008 12:46 am

UpCount

Post by theStack »

Well, that was easy.
I just increased the Java stack memory to 16MB and everything was fine :D

But I guess that was not the intention of the Challenge?
I didn't even think once what this code really does *g*

Greetings,
theStack
Mütze
Posts: 23
Joined: Sun Oct 26, 2008 2:39 pm

Post by Mütze »

Yes, it was easy. But I used gcj instead of javac, which does not have the stack problem.
knox
Posts: 8
Joined: Wed Oct 01, 2008 7:53 pm

Post by knox »

I guess the smartest idea is a simple rewrite as a non-recursive function. :wink:
owBlackhawk
Posts: 2
Joined: Wed Nov 05, 2008 9:16 pm
Contact:

Post by owBlackhawk »

hmm.. I just took the code and made it print me the value of v :D
User avatar
ape
Posts: 6
Joined: Sat Nov 08, 2008 5:28 pm

Post by ape »

i ported(copied) it to c
dotme
Posts: 10
Joined: Sun Nov 16, 2008 6:45 pm

Post by dotme »

I turned the program into a for-loop starting with 1 and solved it with that perl-snippet

Code: Select all

my $depth;
my $cc = 1;
for ( $depth = 1; $depth <= 11589; $depth++ ) {
    $cc = $cc + ($depth % 7) + (((($cc ^ $depth) % 4) == 0) ? 1 : 0);
}
print $cc, "\n";
DocJoe
Posts: 5
Joined: Sun Nov 02, 2008 1:22 pm
Location: Vagina Hole

Post by DocJoe »

ah, seems i spoiled the problem...

had no javac installed and therefore copied it directly to c (nice that the language keywords are that similar so it took a couple of minutes only). Worked out of the box.
Theino
Posts: 8
Joined: Sun Mar 15, 2009 10:35 pm

Post by Theino »

I did it the hard way... figuring out a "simple" program that does the equivalent.

pseudo-code:

Code: Select all

int extra = n%7;
int sum = (n/7)*23 + 2;
switch(extra)
{
case 0:
  sum+=0;
break;
case 1:
  sum+=1;
break;
case 2:
  sum+=3;
break;
case 3:
  sum+=6;
break;
case 4:
  sum+=11;
break;
case 5:
  sum+=17;
break;
case 6:
  sum+=23;
break;
}

return sum;
works for inputs 'n' greater than 7

No need for looping nor recursion... too bad he didn't put a really huge number into the depth.
User avatar
fido2509
Posts: 10
Joined: Thu Jul 16, 2009 8:00 pm

Post by fido2509 »

I don't like java, so I translated the code to PHP:

Code: Select all

function calc($depth) {
	if ($depth == 0) return 1;
	$cc = calc($depth - 1);
	return (double)$cc + ($depth % 7) + (((($cc ^ $depth) % 4) == 0) ? 1 : 0); 
}
echo calc(11589);
Didn't take more than 1 minute to translate and run ;)
r00t
Posts: 1
Joined: Fri Aug 28, 2009 7:56 am

Post by r00t »

my version was:

Code: Select all

<html>
<script language="javascript" type="text/javascript">
function mycalc() {
var plus = new Array(0,1,2,3,5,6,6);
sum =	2302;
j=0;
for (i=700;i<11590;i++) {
	sum+=plus[j];
    j++;
    if (j==7) {j=0;}
    document.writeln("<br/>",i,"->",sum);
  }
}
</script>

<p><script language="javascript" type="text/javascript">mycalc()</script> </p>
</body>
</html>
matter
Posts: 11
Joined: Mon Oct 12, 2009 7:30 am

Post by matter »

fido2509 wrote:I don't like java, so I translated the code to PHP:

Code: Select all

function calc($depth) {
	if ($depth == 0) return 1;
	$cc = calc($depth - 1);
	return (double)$cc + ($depth % 7) + (((($cc ^ $depth) % 4) == 0) ? 1 : 0); 
}
echo calc(11589);
Didn't take more than 1 minute to translate and run ;)
Yup, did exactly the same thing. Good ol' PHP :P
wynksaiddestroy
Posts: 4
Joined: Thu May 07, 2009 10:27 pm

Post by wynksaiddestroy »

The iterative Java function:

Code: Select all

private long calc(int depth) {
		long result = 1;
		for(int i=1; i<=depth; i++) {
			result = result + (i % 7) + ((((result ^ i) % 4) == 0) ? 1 : 0); 
		}
		return result;
	}
zuo
Posts: 2
Joined: Wed Mar 31, 2010 2:36 pm

Post by zuo »

public static void main(String[] args) {
long[] d = new long[11590];
d[0] = 1;
for (int i = 1; i <= 11589; i++) {
d = d[i-1] + (i % 7) + ((((d[i-1] ^ i) & 3) == 0) ? 1 : 0);
}
System.out.println(d[11589]);
}
simply do a memorization and use bitwise operation to do modulo
markobr
Posts: 17
Joined: Thu May 20, 2010 4:09 pm
Location: Tübingen
Contact:

Post by markobr »

I didn't have a java compiler at hand, so I translated it to Perl. Runs without problems and in a fraction of a second. Now what's the challenge?
dobluth
Posts: 1
Joined: Fri Nov 28, 2008 6:41 pm

Post by dobluth »

I just avoided stack issues by using different values, i.e. instead of calc(11589) I ran calc(9000) and changed the code to (29574 was the result of calc(9000)):

Code: Select all

public class UpCount {
	private long calc(int depth) {
		if (depth == 9000) return 29574;
		long cc = calc(depth - 1);
		return cc + (depth % 7) + ((((cc ^ depth) % 4) == 0) ? 1 : 0); 
	}
	public static void main(String[] args) {
		UpCount uc = new UpCount();
		System.out.println(uc.calc(11589));
	}
}
This way I could stepwise increase the "boundary" until at last I had the result :)

Just wondering why so many people wanted to rewrite the recursive code instead of taking advantage of it.
Post Reply