Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:41:10+00:00 2026-06-18T03:41:10+00:00

Basicaly when i run the code it works if i give it a large

  • 0

Basicaly when i run the code it works if i give it a large number (splitting it in prime numbers,but when i want to run it for low numbers it doesnt work) The problem i see is that if i give the program number 8 it doesnt print nothing on the screen.(i think its something to do with 2+2+2+2 ) and if i write 50 it shows all the posibilities WITHOUT repeating 1 prime number on a line

#include <math.h>
#include <stdio.h>
#include <stdlib.h>


#define is_sol  2
#define is_pos  4
#define impos  0

int n, *st;

int prim (int n)
{ 
    if ( n < 2 ) 
        return impos;
    else
        if ( n != 2 && (n % 2) == 0)
            return impos;
        else 
        { long i;
    for(i = 3; i <=sqrt(1.0*n); i +=2)
        if ( n % i == 0)
            return impos;
    }
    return is_pos;                   
}

int test(int h)
{  
    int i;
    if(!h) 
        return (st[h] < 2 ? impos : prim(st[h]));
    if(st[h] <= st[h-1]) 
        return impos;
    int p = st[h];
    if(!prim(p)) 
        return impos;
    int S = 0;
    for(i = 0 ; i <= h ; i++)
        S += st[i];
    if(S == n) 
        return is_sol;
    return (S < n ? is_pos : impos);
}

void print (int h)
{    
    int i;
    for(i = 0 ; i <= h; i++)
        printf("%d ", st[i]);
    printf("\n");
}

void back(int h) 
{ 
    int k;
    for(k = 2; k <= n/2; k++) 
    { 
        st[h] = k;
        int rez = test(h);
        if(rez == is_sol)
            print(h);
        else 
        { 
            if(rez == is_pos)
                back(h+1);
        }
    }
}

int main()
{ 
    printf( "Your number: ");
    scanf("%d",&n);
    st = (int*)malloc(sizeof(int)*(n/2));
    back(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:41:12+00:00Added an answer on June 18, 2026 at 3:41 am

    The condition

    for(k = 2; k <= n/2; k++)
    

    is responsible. If you want to write a number as the sum of distinct primes, for small numbers, the only ways often include one prime larger than n/2 – e.g. the only way to write 8 as the sum of distinct primes is 8 = 3 + 5.

    If you make it k <= n-2, it will work.

    If you want to allow using the same prime several times, as in 8 = 2+2+2+2 or 8 = 2+3+3, you need to change

    if(st[h] <= st[h-1])
    

    to

    if(st[h] < st[h-1])
    

    in test.

    And you should keep track of the sum you have so far to avoid the most blatant inefficiencies. (And store a list of primes instead of checking each number every time.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the following code it basically works how I intend it to,
The code works but looks messy so this might be a code review question
Basically my issue comes down to this, I need to run code on a
Basically I'm trying to run some code (Python 3.2) if a value on a
Basically the question is how do I run gpgpu code in a Metro app.
I have rather large project that uses ICU regex classes. Basically it might run
Basically I want to run a script checking for the existence of a certain
Basiclly, I want to capture audio/video. Run it through a mp4 muxer and save
I have a UITableView that displays a subset of a large number of entities
i have been using System.Timer to run a windows service but have come across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.