#nonfunctional spaghetti prime sieve

26 messages · Page 1 of 1 (latest)

main goblet
#

I kind of messed up and I want to know how badly I messed up

import java.util.*;

public class prime {

    public static int runFirst() {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Iterations: ");

    int I = scanner.nextInt();

    return I;
    }

    static int[] primes = new int[runFirst()];
    
    

    public static void main(String args[]) {
        int I = runFirst();

        primes[0] = 1;
        primes[1] = 2;
        
        for(int n = 3; n <= I; n++) {
            if ((n % 1 == 0) && (n % n == 0)) {

                boolean isPrime = true;

                for(int i = 1; i <= n; i++) {

                    int x = otherNums(n, i);

                        if(x != 0) {
                            isPrime = false;
                        }
                }
                if(isPrime == true) {
                    System.out.println(n);
                    primes[n-1] = n;
                }
            }
        }

    }

    public static int otherNums(int a, int b) {
        int c = 0;
        
            if ((a % prime.primes[b]) != 0) {
                c++;
            }

        return c;
    }

    
}
sweet minnow
#
  1. if(isPrime == true) -> if(isPrime)
  2. ...void main(String args[]) -> ...void main(String[] args)
  3. (n % n == 0) is always true because it's always equals to 1 so it's redundant in conjunction.
  4. int I = scanner.nextInt(); return I; -> return scanner.nextInt()
#

Btw I'm not sure what ur code is supposed to do. Can u explain?

dull perch
#

ur code is messed up because 1 is NOT a prime!!

#

if one is a prime, prime factorisation is impossible because every number has an infinite number of primes that factor in

#

dont use I as a variable ........ firstly nobody knows what that means when they read the code and ur certainly not using it as an iterator here
and secondly, in java an ALL CAPS variable represents a constant thats been defined
and thirdly a variable is supposed to be written in lowerPascalCase where each word except the first starts with an uppercase character to differntiate between words (same as method names)

#

give a name that explains to readers what the variable is for like upperLimit of largestNumber or checkUntil for example

mint saddle
#
import java.util.Scanner;

public class Prime {
    public static int runFirst() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Iterations: ");
        int i = scanner.nextInt();
        scanner.close();
        return i;
    }

        static int[] primes;// = new int[runFirst()];
        
        public static void main(String[] args) {
            int upperLimit = runFirst();
            primes = new int[upperLimit];
            primes[0] = 2;
            int primeIndex = 1;
            boolean isPrime;

            for(int n = 3; n <= upperLimit; n++) {
                isPrime = true;
                for(int i = 2; i < n; i++) {
                    //int x = otherNums(n, i);
                    if(n % i == 0) {
                        isPrime = false;
                        break;
                    }
                    //if(x != 0) isPrime = false;
                }
                if(isPrime) {
                    primes[primeIndex] = n;
                    primeIndex++;
                }
            }
            System.out.print("Primes: ");
            for(int i = 0; i < primeIndex; i++) {
                System.out.print(primes[i]);
                if(i < primeIndex-1) 
                System.out.print(", ");
            }
        }
}
#

And your class name should start with a capital letter.

mint saddle
knotty sable
#

after each nesting u add 4 spaces

#

ideally

mint saddle
#

That's normal. When I add code inside some block I either press Tab or add 4 spaces. That'scstandart for most coders.

#

Also when copy from eclipse and paste here discord adds some extra space. And I don't bother deleting it.

knotty sable
#

what u said sounds very magical to me but whatever

dull perch
#

intelliJ or eclipse

#

(i vote intelliJ)

knotty sable
#

and only in a certain area?

mint saddle
# dull perch use an IDE, then the indentation is automatic, u can set it AND u can correct th...

I am using IDE. Eclipse. But I never used shortcut to auto-align my code. I knew it exists but I always used to align manually. Actually it is shortcut Ctrl+Shift+F.

package prime;

import java.util.Scanner;

public class Prime {
    private static int runFirst() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Iterations: ");
        int i = scanner.nextInt();
        scanner.close();
        return i;
    }

    static int[] primes;

    public static void main(String[] args) {
        int upperLimit = runFirst();

        primes = new int[upperLimit];

        primes[0] = 2;

        int primeIndex = 1;
        boolean isPrime;
        for (int n = 3; n <= upperLimit; n++) {
            isPrime = true;

            for (int i = 2; i < n; i++) {
                if (n % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                primes[primeIndex] = n;
                primeIndex++;
            }
        }
        printPrimes(primeIndex);
    }
}

Just when I paste here then discord adds one more tab space. And makes the indentation look horrible. Every line that had tab in code here becomes double pushed. I don't know how for you all it pastes correctly. Here I have to manually delete those "tabs". I hate this discord behavior.

#

Oh, after all it pasted correctly.

#

Just while I was typing it was doubled.