Jump to content

The clue is in the "for(i=2..." loop, or more specifically the "for (j=i+1.." loop. 

It sets certain numbers in the array 'a' to zero, as to not print them out at the end.

 

What is something the numbers you mentioned have in common why they do pass this modulo (%) if-statement? 

Go by each number (1-15), write the value of i and j in these loops and check why some pass and others don't pass this test (doublecheck what the modulo statement does if you're not sure what it's checking).

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

This is a standard math thing, but let's not Google it. 

 

Look at the numbers at the beginning. 

i and j are whole numbers as they are integers. 

 

We also have an array which wasn't declared but we know the array is called a. 

The first for loop sets the values of a to what?

List that out. 

 

Something to pay attention to is that there is only one i and one j. 

Where are the values for these variables changed? 

Circle them and post a picture. 

Write out on paper every time the array a is changed and what every value is. 

 

Go through the program by hand with a piece of paper and a pen. 

 

Doing this, you'll understand how they relate. 

If you can power through this the hard way, you'll be much better for it. 

Link to post
Share on other sites

9 hours ago, Liam mallka said:

the output that I got is 1 2 3 5 7 11 12 but I cant connect the numbers.

N=15

Then you did something wrong, it should print 1, 2, 3, 5, 7, 11, 13.

 

Technically 1 should not be in there either.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

On 11/18/2022 at 10:49 PM, Sakuriru said:

True, so it doesn't print the prime numbers.

 

What a freakin mess.

Here's a modern adaption of the algorithm that produces the expected values 😆

final int n = 17;
IntStream.rangeClosed(2, n)
    .filter(i -> IntStream.range(2, i).noneMatch(j -> i % j == 0))
    .forEach(System.out::println);

 

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

  • 2 weeks later...
On 11/18/2022 at 6:14 AM, Liam mallka said:

I got this code in a test where I can use friends, the internet, etc... and I need to explain what problem this code solves, however I cant figure it out

the output that I got is 1 2 3 5 7 11 12 but I cant connect the numbers.

N=15

 

image.png

I know this is super late, but in case anyone else stumbles across this mess, there's a blatant syntax error in the conditional for the last loop, on top of the compiler error for the array a neither being declared nor initialized.  As far as I'm aware,

-<=

isn't an operator, but maybe my Java is rusty.  Whoever wrote this question needed to not use Word or whatever other rich text editor to write it.  I seriously hope that wasn't the answer the exam administrator was looking for.  I hate trick questions.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×