Project Euler #12: Highly divisible triangular number

Sort by

recency

|

121 Discussions

|

  • + 0 comments

    include

    include

    include

    using namespace std;

    int main() { const int MAX_PRIMES = 1000; vector primes(MAX_PRIMES, 0); primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; int count1 = 4; long long num = 9; while(count1 < MAX_PRIMES) { bool isprime = true; int sqrt_num = sqrt(num);

        for(int i = 0; primes[i] <= sqrt_num && i < count1; i++) {
            if(num % primes[i] == 0) {
                isprime = false;
                break;
            }
        }
        if(isprime) {
            primes[count1] = num;
            count1++;
        }
        num += 2;
    }
    
    int t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
    
        long long sum = 1;
        long long result = 1;
        long long j = 2;
    
        while(true) {
            sum += j;
            long long temp = sum;
            result = 1;  
            for(int k = 0; k < count1 && primes[k] > 0; k++) {
                int count2 = 1;
                while(temp % primes[k] == 0) {
                    temp /= primes[k];
                    count2++;
                }
                result *= count2;
                if(temp == 1) {
                    break;
                }
            }
            if(result > n) {
                break;
            }
            j++;
        }
        cout << sum << endl;
    }
    return 0;
    

    }

  • + 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);
        }
    }
    

    }