PDA

View Full Version : Java Question


SpaceTrekkie
29-January-2006, 10:57 PM
hey all,

I have a question for anyone who knows java. I need to write a for loop that will print out: 3 5 7 9 3 5 7 9. I can't seem to get that pattern no matter what i try. If anyone has any idea please let me know!

thanks in advance,
ST

randb
30-January-2006, 08:49 AM
how many repetitions of the pattern (3 5 7 9) do you need?

i = 0
for i < 2
print ("3 5 7 9")
i = i + 1


This isnt java....cuz I'm not sure of the functions used...but you could use this to get an idea.
Good Luck!!!!

randb
30-January-2006, 08:55 AM
if you want to generate the numbers, you'll need 2 loops!!! one for number of repetitions and the other for generating numbers!!! 2 variables.... one for number of repetitions and the other with an initial value of 3 and inrements of 3 till it is less than 11.

k = 0
for k < 2
i = 3
for i < 11
print i
i = i + 2
k++

snarkophilus
30-January-2006, 09:52 AM
if you want to generate the numbers, you'll need 2 loops!!! one for number of repetitions and the other for generating numbers!!! 2 variables.... one for number of repetitions and the other with an initial value of 3 and inrements of 3 till it is less than 11.

k = 0
for k < 2
i = 3
for i < 11
print i
i = i + 2
k++

Two loops aren't necessary if you use the modulus operator. Just take k from 1 to 19, step 2, print k % 10, and insert a statement before the print that increments k by 2 if k % 10 == 1. If you don't want to do it with the step of 2 (I know people who refuse to do this), go k from 1 to 9, increment if k % 5 == 0, and print (2*k + 1) % 10.

Your way is probably more efficient, though. :)

SpaceTrekkie
30-January-2006, 11:22 AM
thanks for your help all! i ended up doing it in one loop with a mod 8 and adding a whole bunch in and stuff. Probably not the most efficient...but i got then numbers!

st

randb
31-January-2006, 07:19 AM
Two loops aren't necessary if you use the modulus operator. Just take k from 1 to 19, step 2, print k % 10, and insert a statement before the print that increments k by 2 if k % 10 == 1. If you don't want to do it with the step of 2 (I know people who refuse to do this), go k from 1 to 9, increment if k % 5 == 0, and print (2*k + 1) % 10.

Your way is probably more efficient, though. :)

I didnt come across this in my C/C++ classes that I took 2 years ago...lol... I've never used JAVA before... But smaller the code, better it is!!!

snarkophilus
31-January-2006, 09:53 AM
thanks for your help all! i ended up doing it in one loop with a mod 8 and adding a whole bunch in and stuff. Probably not the most efficient...but i got then numbers!

st

Using mod 8... very good! I didn't even think of that, but of course that's the way to go.

for i from 1 to 9
print ((2*i - 1) % 8) + 2

Again, the double for loop might be faster... but it might not. It depends upon both your processor and your compiler.

Carnifex
31-January-2006, 11:55 AM
I didnt come across this in my C/C++ classes that I took 2 years ago...lol... I've never used JAVA before... But smaller the code, better it is!!!

WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program :dance:

Robert Andersson
01-February-2006, 09:32 AM
WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program :dance:
In obvious cases, sure, but otherwise often no. On modern processors, conditionals tend to trash the instruction pipeline (branch prediction). It is often faster to use "continuous" arithmetics, than trying to save a few instructions with conditionals.

Like this example. Avoiding the lengthy calculation if x is zero won't make this code faster, even if x is zero most of the time.
float f(int x, float y)
{
if(x == 0) {
return 0.0f;
}
return (y / 2 + 5 + x) / 2.5f * x;
}

Robert Andersson
01-February-2006, 09:44 AM
for i from 1 to 9
print ((2*i - 1) % 8) + 2
Perhaps cleaner / more readable:
for(i = 0; i < x; ++i) {
n = i % 4 * 2 + 3;
}
EDIT: maybe this is still better; a matter of taste:
for(i = 0; i < 2 * x; i += 2) {
n = i % 8 + 3;
}

worzel
01-February-2006, 12:11 PM
or maybe:

for(i = 0; i < 2 * x; i += 2) {
n = (i & 7) + 3;
}

randb
03-February-2006, 01:09 AM
WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program :dance:

well...i guess so....I'm not sure, but while writing small programs, it doesn't really make a difference. (One has to type less, if a program is small...so smaller is better :D )