A Prime Conjecture CodeChef Solution

Problem – A Prime Conjecture CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.
<<Previous CodeChef Problem Next Codechef Problem>>

A Prime Conjecture CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;

const int N = 1e6;
int prime[N];
void primecal(){
    prime[1] = 1;
    for(int i = 2; i <=sqrt(N);i++){
        if(prime[i]==0){
            for(int j = i*i;j<N;j+=i){
                prime[j] = 1;
            }
        }
    }
}
int main() {
    // your code goes here
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    freopen("output.in","w",stdout);
#endif
    primecal();
    
    while(1){

        int n;
        cin>>n;
        if(n==0)
            break;
        int cb = cbrt(n)+1;
        int sq = sqrt(n)+1;
        bool is = false;
        int i;
        for(i = 2 ; i <= cb && !is;i++){
            if(!prime[i]){
                for (int j = 2; j <= sq; j++) {
                    if(!prime[j]){
                        int c = n - j*j - i*i*i;
                        if(c>=2&&prime[c]==0){
                            cout<<c<<" "<<j<<" "<<i<<endl;
                            is = true;
                            break;
                        }
                    }
                }
            }
        }
        if(!is){
            cout<<"0 0 0"<<endl;

        }
    }
    return 0;
}

A Prime Conjecture CodeChef Solution in PYTH 3

# cook your dish here
from math import sqrt
def is_prime(n):
    for i in range(2,int(sqrt(n))+1):
        if n%i==0:
            return False
    return True
l=[]
for i in range(2,1000):
    z=is_prime(i)
    if z==True:
        l.append(i)
    else:
        continue
k=[]
while True:
    c=int(input())
    if c==0:
        break
    else:
        k.append(c)
for j in k:
    flag=0
    for s in l:
        for h in l:
            d=j-(s**2+h**3)
            if d>1:
                if is_prime(d)==True:
                    print(d,s,h)
                    flag=1
                    break
        if flag==1:
            break
    
    if flag==0:
        print(0,0,0)
            

A Prime Conjecture CodeChef Solution in C

#include<stdio.h>
int isprime(int n)
{
	int i;
	if((n<2) || ((!(n&1)) && (n!=2)))
		return 0;
	for(i=3;i*i<=n;i+=2)
		if(!(n%i))
			return 0;
	return 1;
} 
int main()
{
	int found,p1,p2,p3,pcube,N;
	scanf("%d",&N);
	while(N)
	{
		found=0;
		for(p3=100;p3>0;p3--)
		{
			if(( isprime(p3) && (pcube=p3*p3*p3)<N))
			{
				for(p2=2;(p2*p2+pcube)<N;p2++)
				{
					p1=N-pcube-p2*p2;
					if(isprime(p2) && isprime(p1))
					{
						found=1;
						break;
					}
				}
				if(found)
					break;
			}
		}
		if(!found)
			p1=p2=p3=0;
		printf("%d %d %d\n",p1,p2,p3);
		scanf("%d",&N);
	}
	return 0;
} 

A Prime Conjecture CodeChef Solution in JAVA

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

class CodeChef{
    public static void main(String[] args) {
        try {
            FastReader in=new FastReader();
            FastWriter out = new FastWriter();
            
            while(true){
            int testCases = in.nextInt();
            if(testCases == 0) break;
                int[] curAns = new int[3];
                solve(testCases, curAns);
                out.println(curAns[0] + " " + curAns[1] + " " + curAns[2]);
            }
            out.close();
    } catch (Exception e) {
            for(int i = 0 ; i < e.getStackTrace().length ; i++){
                System.out.println(e + " at line " + e.getStackTrace()[i].getLineNumber());
            }
            return;
        }
    }
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br=new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while(st==null || !st.hasMoreTokens()){
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str="";
            try {
                str=br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static class FastWriter {
		private final BufferedWriter bw;

		public FastWriter() {
			this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
		}

		public void print(Object object) throws IOException {
			bw.append("" + object);
		}

		public void println(Object object) throws IOException {
			print(object);
			bw.append("\n");
		}

		public void close() throws IOException {
			bw.close();
		}
	}
	static class Pair implements Comparable<Pair>{
	    
	    Pair(){
	        
	    }
	    public int compareTo(Pair that){
	        return 0;
	    }
	}
	static int[] par;
	static int[] rank;
	static int find(int a){
	    if(par[a] == a) return a;
	    else return par[a] = find(par[a]);
	}
	static void merge(int a, int b){
	    int para = find(a);
	    int parb = find(b);
	    if(para == parb) return;
	    if(rank[para] < rank[parb]){
	            par[para] = parb;
	    }
	    else if(rank[parb] < par[a]){
	            par[parb] = para;
	    }
	    else{
	            par[parb] = para;
	            ++rank[para];
	    }
	}
	static long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
    static boolean biPartite(ArrayList<ArrayList<Integer>> adj,int V){
        HashMap<Integer,Integer> hm=new HashMap<>();
        for(int i=1;i<=V;i++){
            if(hm.get(i)==null) if(!(modDFS(i,adj,hm,0))) return false;
        }
        return true;
    }
    static boolean modDFS(int V,ArrayList<ArrayList<Integer>> adj,HashMap<Integer,Integer> hm,int c){
        if(hm.get(V)!=null) if(hm.get(V)!=c) return false; else return true;
        hm.put(V,c);
        
            for(Integer k:adj.get(V)){
                if(c==1){
                    if(!modDFS(k,adj,hm,0)) return false;
                }
                else if(!modDFS(k,adj,hm,1)) return false;
            }
        
        return true;
    }
    static void solve(int n, int[] ans){
        for(int p3 = 100 ; p3 > 0 ; p3--){
            if(p3 * p3 * p3 < n && isPrime(p3)){
                for(int p2 = 2 ; p2*p2 + p3*p3*p3 < n ; p2++){
                    int p1 = n - (p3*p3*p3 + p2*p2);
                    if(isPrime(p2) && isPrime(p1)){
                        ans[0] = p1; ans[1] = p2; ans[2] = p3;
                        return;
                    }
                }
            }
        }
    }
    static boolean isPrime(int n){
        if(n < 2) return false;
        for(int i = 2 ; i * i <= n ; i++) if(n % i == 0) return false;
        return true;
    }
}

A Prime Conjecture CodeChef Solution in PYPY 3

def isPrime(n):
    if n < 2: 
        return False
    if n < 4:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    for i in range(5, int(n**0.5)+1, 6):
        if n % i == 0 or n % (i+2) == 0:
            return False
    return True    

SoE = [True for i in range(1001)]
SoE[0], SoE[1] = False, False
for n in range(4, 1001, 2):
    SoE[n] = False
p = 3
while p*p <= 1000:
    if SoE[p] == True:
        for n in range(p*2, 1001, p):
            SoE[n] = False
    p += 2
p_sqrs = [p*p for p in range(999,-1,-1) if SoE[p]]
p_cubs = [p*p*p for p in range(99,-1,-1) if SoE[p]]

while 1:
    num = int(input())
    if num == 0:
        break
    res = False
    for ps in p_sqrs:
        for pc in p_cubs:
            if isPrime(num - ps - pc):
                print(num - ps - pc, round(ps**0.5), round(pc**(1/3)))
                res = True
                break
        if res:
            break
    if not res:
        print(0, 0, 0)

A Prime Conjecture CodeChef Solution in PYTH

N = 10**6 # value of N required
def primesieve(N):
	n = N+1
	sieve = [True] * (n/2)
	for i in xrange(3,int(n**0.5)+1,2):
		if sieve[i/2]:
			sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)
		# endif
	# endfor i
	return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]
# end fun
primes = primesieve(N)
SQ = []
CB = []
q = 0
p = primes[0]
while p < 1000:
	n = p*p
	SQ.append(n)
	if p < 100:
		n = p*n
		CB.append(n)
	# endif
	q += 1
	p = primes[q]
# endwhile
Z = []
a = len(CB)
b = len(SQ)
for k in range(a):
	n1 = CB[k]
	tot = n1
	i = 0
	while (i < b) and (tot < N):
		tot = n1 + SQ[i]
		if tot < N:
			Z.append([tot,primes[i],primes[k]])
		# endif
		i += 1
	# endwhile
# endfor k
Z.sort()
S = set(primes)
v = -1
while v != 0:
	v = int(raw_input())
	if v > 0:
		found = False
		n = 0
		r = 0
		while (not found) and (n < v):
			n = Z[r][0]
			if v-n in S:
				found = True
				print v-n, Z[r][1], Z[r][2]
			# endif
			r += 1
		# endwhile
		if not found:
			print '0 0 0'
		# endif
	# endif
# endwhile

A Prime Conjecture CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication119
{
    class Program
    {

        public bool IsPrime(int candidate)
        {
            if ((candidate & 1) == 0)
            {
                if (candidate == 2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            for (int i = 3; (i * i) <= candidate; i += 2)
            {
                if ((candidate % i) == 0)
                {
                    return false;
                }
            }
            return candidate != 1;
        }




        //Console.WriteLine(Math.Floor(Math.Pow((double)N, (double)(1.0 / 3.0))));
        //Console.WriteLine(Math.Floor(Math.Sqrt((double)N)));


        static void Main(string[] args)                                                     //A Prime Conjecture
        {
            Program p = new Program();
            while (true)
            {
                int N, x, y, z, final = 0,N1;
                Int32.TryParse(Console.ReadLine(), out N);
                if (N == 0)
                    break;

                N1 = (int)Math.Floor(Math.Pow((double)N, (double)(1.0 / 3.0)));
                for (x = N1; x > 1; x--)
                {
                    if (x * x * x < N && p.IsPrime(x))
                    {
                        for (y = 2; y * y + x * x * x < N; y++)
                        {
                            z = N - x * x * x - y * y;
                            if (p.IsPrime(y) && p.IsPrime(z))
                            {
                                Console.WriteLine("{0} {1} {2}", z, y, x);
                                final=1;
                                break;
                            }
                        }
                        
                    }
                    if (final == 1)
                        break;
                }
                if (final != 1)
                {
                    x = y = z = 0;
                    Console.WriteLine("{0} {1} {2}", z, y, x);
                }

            }

        }
    }
}

A Prime Conjecture CodeChef Solution in GO

package main

import (
	"fmt"
	"sort"
)

var MAX_N = 1000000

func main() {

	p1 := sieve(MAX_N)
	p2, p3 := squareAndCube(p1)

	var n int
	for {
		fmt.Scanf("%d", &n)
		if n <= 0 {
			break
		}
		a, b, c := findAnswer(n, p1, p2, p3)
		fmt.Printf("%d %d %d\n", a, b, c)
	}
}

func findAnswer(n int, p1, p2, p3 []int) (int, int, int) {
	for i := 0; i < len(p3); i++ {
		a := p3[i]
		a3 := a * a * a
		if a3 >= n {
			break
		}
		for j := 0; j < len(p2); j++ {
			b := p2[j]
			b2 := b * b
			if a3+b2 >= n {
				break
			}
			c := n - a3 - b2
			k := sort.Search(len(p1), func(k int) bool {
				return p1[k] >= c
			})

			if k < len(p1) && p1[k] == c {
				return c, b, a
			}
		}
	}

	return 0, 0, 0
}

func squareAndCube(p1 []int) ([]int, []int) {
	p2, p3 := make([]int, 0, 1000), make([]int, 0, 100)

	for i := 0; i < len(p1); i++ {
		num := p1[i]
		if num*num >= MAX_N {
			break
		}
		p2 = append(p2, num)
		if num*num*num < MAX_N {
			p3 = append(p3, num)
		}
	}

	return p2, p3
}
func sieve(n int) []int {
	flags := make([]bool, n+1)
	ps := make([]int, 0, n)

	flags[2] = false

	for i := 2; i <= n; i++ {
		if flags[i] {
			continue
		}
		ps = append(ps, i)
		for j := 2 * i; j <= n; j += i {
			flags[j] = true
		}
	}

	return ps
}
A Prime Conjecture CodeChef Solution Review:

In our experience, we suggest you solve this A Prime Conjecture CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the A Prime Conjecture CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this A Prime Conjecture CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *