Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,623 questions

55,358 answers

573 users

How to find two prime numbers that, when concatenated, form another prime number in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class PrimeConcat
{
    // Function to check primality
    static bool IsPrime(long n) {
        if (n < 2) return false;
        if (n % 2 == 0 && n != 2) return false;

        for (long i = 3; i * i <= n; i += 2) {
            if (n % i == 0) return false;
        }
        
        return true;
    }

    // Concatenate two integers
    static long Concat(int a, int b) {
        string s = a.ToString() + b.ToString();
        
        return long.Parse(s);
    }

    static void Main()
    {
        int limit = 12; // you can increase this
        List<int> primes = new List<int>();

        // Generate primes up to limit
        for (int i = 2; i <= limit; i++) {
            if (IsPrime(i)) {
                primes.Add(i);
            }
        }

        // Check pairs
        for (int i = 0; i < primes.Count; i++) {
            for (int j = 0; j < primes.Count; j++) {
                if (i == j) continue; // skip same prime if you want
                long num = Concat(primes[i], primes[j]);
                if (IsPrime(num)) {
                    Console.WriteLine($"{primes[i]} + {primes[j]} -> {num} is prime");
                }
            }
        }
    }
}



/*
run:

2 + 3 -> 23 is prime
2 + 11 -> 211 is prime
3 + 7 -> 37 is prime
3 + 11 -> 311 is prime
5 + 3 -> 53 is prime
7 + 3 -> 73 is prime
11 + 3 -> 113 is prime

*/

 



answered Nov 28, 2025 by avibootz
...