Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:31:16+00:00 2026-06-15T07:31:16+00:00

I’m looking for a J code to do the following. Suppose I have a

  • 0

I’m looking for a J code to do the following.

Suppose I have a list of random integers (sorted),
2 3 4 5 7 21 45 49 61
I want to start with the first element and remove any multiples of the element in the list then move on to the next element cancel out its multiples, so on and so forth.

Thus the output
I’m looking at is 2 3 5 7 61. Basically a Sieve Of Eratosthenes. Would appreciate if someone could explain the code as well, since I’m learning J and find it difficult to get most codes 🙁

Regards,
babsdoc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:31:18+00:00Added an answer on June 15, 2026 at 7:31 am

    It’s not exactly what you ask but here is a more idiomatic (and much faster) version of the Sieve.

    Basically, what you need is to check which number is a multiple of which. You can get this from the table of modulos: |/~

    l =: 2 3 4 5 7 21 45 49 61
    |/~ l
      0 1 0 1 1  1  1  1  1
      2 0 1 2 1  0  0  1  1
      2 3 0 1 3  1  1  1  1
      2 3 4 0 2  1  0  4  1
      2 3 4 5 0  0  3  0  5
      2 3 4 5 7  0  3  7 19
      2 3 4 5 7 21  0  4 16
      2 3 4 5 7 21 45  0 12
      2 3 4 5 7 21 45 49  0
    

    Every pair of multiples gives a 0 on the table. Now, we are not interested in the 0s that correspond to self-modulos (2 mod 2, 3 mod 3, etc; the 0s on the diagonal) so we have to remove them. One way to do this is to add 1s on their place, like so:

    =/~ l
      1 0 0 0 0 0 0 0 0
      0 1 0 0 0 0 0 0 0
      0 0 1 0 0 0 0 0 0
      0 0 0 1 0 0 0 0 0
      0 0 0 0 1 0 0 0 0
      0 0 0 0 0 1 0 0 0
      0 0 0 0 0 0 1 0 0
      0 0 0 0 0 0 0 1 0
      0 0 0 0 0 0 0 0 1
    (=/~l) + (|/~l)
      1 1 0 1 1  1  1  1  1
      2 1 1 2 1  0  0  1  1
      2 3 1 1 3  1  1  1  1
      2 3 4 1 2  1  0  4  1
      2 3 4 5 1  0  3  0  5
      2 3 4 5 7  1  3  7 19
      2 3 4 5 7 21  1  4 16
      2 3 4 5 7 21 45  1 12
      2 3 4 5 7 21 45 49  1
    

    This can be also written as (=/~ + |/~) l.

    From this table we get the final list of numbers: every number whose column contains a 0, is excluded.

    We build this list of exclusions simply by multiplying by column. If a column contains a 0, its product is 0 otherwise it’s a positive number:

    */ (=/~ + |/~) l
       256 2187 0 6250 14406 0 0 0 18240
    

    Before doing the last step, we’ll have to improve this a little. There is no reason to perform long multiplications since we are only interested in 0s and not-0s. So, when building the table, we’ll keep only 0s and 1s by taking the “sign” of each number (this is the signum:*):

    * (=/~ + |/~) l
      1 1 0 1 1 1 1 1 1
      1 1 1 1 1 0 0 1 1
      1 1 1 1 1 1 1 1 1
      1 1 1 1 1 1 0 1 1
      1 1 1 1 1 0 1 0 1
      1 1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 1 1
    

    so,

    */ * (=/~ + |/~) l
      1 1 0 1 1 0 0 0 1
    

    From the list of exclusion, you just copy:# the numbers to your final list:

    l #~ */ * (=/~ + |/~) l
       2 3 5 7 61
    

    or,

    (]#~[:*/[:*=/~+|/~) l
       2 3 5 7 61
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.