Project Euler #12: Highly divisible triangular number

Sort by

recency

|

120 Discussions

|

  • + 0 comments

    BY TANUSHREE SARKAR

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            int[] primes = new int[1000];
            primes[0] = 2;
            primes[1] = 3;
            primes[2] = 5;
            primes[3] = 7;
            int count1 = 4;
            int num = 9;
    
            while (num <= 1000000) {
                boolean isPrime = true;
                for (int i = 0; i < count1; i++) {
                    if (num % primes[i] == 0) {
                        isPrime = false;
                        break;
                    } else if (primes[i] > (num / 2)) {
                        break;
                    }
                }
                if (isPrime) {
                    primes[count1] = num;
                    count1++;
                    if (count1 == 1000) {
                        break;
                    }
                }
                num += 2;
            }
    
            Scanner in = new Scanner(System.in);
            int t = in.nextInt();
            for (int a0 = 0; a0 < t; a0++) {
                long n = in.nextLong();
    
                int j = 2;
                long result = 1;
                long sum = 1;
                int count2 = 1;
    
                while (true) {
                    sum += j;
                    long temp = sum;
    
                    for (int k = 0; k < 1000; k++) {
                        count2 = 1;
                        while (true) {
                            if (temp % primes[k] == 0) {
                                temp /= primes[k];
                                count2++;
                            } else {
                                break;
                            }
                        }
                        result *= count2;
                        if (temp == 1) {
                            break;
                        }
                    }
                    if (result > n) {
                        break;
                    } else {
                        result = 1;
                    }
                    j++;
                }
                System.out.println(sum);
            }
        }
    }
    
  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {

    int [] primes = new int[1000];

    primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; int count1 = 4; int num = 9;

    int i = 0;

    mainl: while(num <= 1000000) {

    for(i=0;i<count1;i++) {
        if(num % primes[i] == 0) {
            break;
        }
        else if(primes[i] > (num/2)) {
            primes[count1] = num;
            count1++;
    
            if(count1 == 1000) {
                break mainl;
            }
            break;
        }
    }
    
    if(i == count1) {
        primes[count1] = num;
        count1++;
    
        if(count1 == 1000) {
            break mainl;
        }
    }
    
    num += 2;
    

    }

    Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int a0 = 0; a0 < t; a0++){ long n = in.nextLong();

    int j = 2;
    

    long result = 1; long sum = 1; int count2 = 1;

    while(true) {

    sum += j;
    long temp = sum;
    
    loop1:
    for(int k=0;k<1000;k++) {
        count2 = 1;
        while(true) {
    
            if(temp % primes[k] == 0) {
                temp /= primes[k];
                count2++;
            }
            else {
                break;
            }
        }
        result *= count2;
        if(temp == 1) {
            break loop1;
        }
    }
    if(result > n) {
        break;
    }
    else {
        result = 1;
    }
    
    j++;
    

    }

    System.out.println(sum); }}}

  • + 0 comments

    c# using System;

    namespace Solution { class Program { static void Main(string[] args) { int[] primes = new int[1000];

            primes[0] = 2;
            primes[1] = 3;
            primes[2] = 5;
            primes[3] = 7;
            int count1 = 4;
            int num = 9;
    
            int i = 0;
    
            while (num <= 1000000 && count1 < primes.Length) // Ensure count1 doesn't exceed array bounds
            {
                for (i = 0; i < count1; i++)
                {
                    if (num % primes[i] == 0)
                    {
                        break;
                    }
                    else if (primes[i] > (num / 2))
                    {
                        primes[count1] = num;
                        count1++;
    
                        if (count1 == 1000)
                        {
                            break;
                        }
                        break;
                    }
                }
    
                if (i == count1)
                {
                    primes[count1] = num;
                    count1++;
    
                    if (count1 == 1000)
                    {
                        break;
                    }
                }
    
                num += 2;
            }
    
            int t = Convert.ToInt32(Console.ReadLine());
            for (int a0 = 0; a0 < t; a0++)
            {
                long n = Convert.ToInt64(Console.ReadLine());
    
                int j = 2;
                long result = 1;
                long sum = 1;
                int count2 = 1;
    
                while (true)
                {
                    sum += j;
                    long temp = sum;
    
                    for (int k = 0; k < count1; k++) // Iterate up to count1
                    {
                        count2 = 1;
                        while (true)
                        {
                            if (temp % primes[k] == 0)
                            {
                                temp /= primes[k];
                                count2++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        result *= count2;
                        if (temp == 1)
                        {
                            break;
                        }
                    }
                    if (result > n)
                    {
                        break;
                    }
                    else
                    {
                        result = 1;
                    }
    
                    j++;
                }
    
                Console.WriteLine(sum);
            }
        }
    }
    

    }

  • + 0 comments

    java code 100%**

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        int [] primes = new int[1000];
    
        primes[0] = 2;
        primes[1] = 3;
        primes[2] = 5;
        primes[3] = 7;
        int count1 = 4;
        int num = 9;
    
        int i = 0;
    
        mainl:
        while(num <= 1000000) {
    
            for(i=0;i<count1;i++) {
                if(num % primes[i] == 0) {
                    break;
                }
                else if(primes[i] > (num/2)) {
                    primes[count1] = num;
                    count1++;
    
                    if(count1 == 1000) {
                        break mainl;
                    }
                    break;
                }
            }
    
            if(i == count1) {
                primes[count1] = num;
                count1++;
    
                if(count1 == 1000) {
                    break mainl;
                }
            }
    
            num += 2;
        }
    
    
    
    
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            long n = in.nextLong();
    
    
            int j = 2;
        long result = 1;
        long sum = 1;
        int count2 = 1;
    
    
        while(true) {
    
            sum += j;
            long temp = sum;
    
            loop1:
            for(int k=0;k<1000;k++) {
                count2 = 1;
                while(true) {
    
                    if(temp % primes[k] == 0) {
                        temp /= primes[k];
                        count2++;
                    }
                    else {
                        break;
                    }
                }
                result *= count2;
                if(temp == 1) {
                    break loop1;
                }
            }
            if(result > n) {
                break;
            }
            else {
                result = 1;
            }
    
            j++;
    
        }
    
        System.out.println(sum);
        }
    }
    

    }

  • + 0 comments

    Test case 6 & 7 fails

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int queries = scanner.nextInt();
            for (int i = 0; i < queries; i++) {
                int n = scanner.nextInt();
                System.out.println(findTriangleNumberWithDivisors(n));
            }
            scanner.close();
        }
    
        private static long findTriangleNumberWithDivisors(int n) {
            int maxDivisors = 0;
            long result = 0;
            int currentNumber = 1;
    
            while (true) {
                long triangularNumber = calculateTriangularNumber(currentNumber);
                int divisors = countDivisors(triangularNumber);
                if (divisors > maxDivisors) {
                    maxDivisors = divisors;
                    result = triangularNumber;
                }
                if (maxDivisors > n) {
                    break;
                }
                currentNumber++;
            }
            return result;
        }
    
        private static long calculateTriangularNumber(int n) {
            return n * (n + 1) / 2;
        }
    
        private static int countDivisors(long number) {
            int count = 0;
            for (int i = 1; i <= Math.sqrt(Double.valueOf(number)); i++) { // Use Math.sqrt(double) instead of Math.sqrt(long)
                if (number % i == 0) {
                    if (number / i == i) {
                        count++;
                    } else {
                        count += 2;
                    }
    
                }
            }
            return count;
        }
    }
    

    `