code help

C, C++, Visual C++, C++.Net Topics
Post Reply
ghostrider_gr
Sergeant
Sergeant
Posts: 17
Joined: Mon Apr 19, 2010 1:01 am

code help

Post by ghostrider_gr » Tue Apr 27, 2010 3:40 pm

next one: i have found alla the primes (previous topic) and now i want in a list all the numbers (p^2^k)<=Upper limit where p the prime, k a nonnegative integer.. for example if upper limit is 24 then i want :

[2,2^2=4,2^3=8,16,3,9,5,7,11,13,17,23]

Code: Select all

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define UPPER_LIMIT 42
    #define B 5

    typedef struct primes_ {
       int no;
       struct primes_ * next;
    } PrimesList;


    int main (){

       int i,count, flag1;
       PrimesList *list, *temp, *initial;

       list = (PrimesList*)malloc(sizeof(PrimesList));
       list->no = 2;
       list->next = NULL;
       temp = list;

       count = 1;

       i = 3;

       while (i <= B) {

          initial = list;
          flag1 = 0;

          while ( ((initial->next) != NULL) && (flag1 == 0) ) {
             if ((i % initial->no) == 0){
                flag1 = 1; // prime factor exists -> skip this number
             }
             else{
                initial = initial->next; // advance to next prime
             }
          }

          if (flag1 == 0) {
             temp->next = (PrimesList*)malloc(sizeof(PrimesList));
             temp = temp->next;
             temp->no = i;
             temp->next = NULL;
             count++;
          }
          i += 2;
       }

    temp = list;
       
    PrimesList *listS, *tempS;
    listS=(PrimesList*)malloc(sizeof(PrimesList));
    tempS=listS;
    
    tempS->no=1;
    tempS->next=(PrimesList*)malloc(sizeof(PrimesList));
    tempS=tempS->next;
    tempS->next=NULL;
    
    while (temp->next !=NULL) {
          int p=temp->no;
          int k=0,flagS=0;
          while (flagS==0) {
                int z=pow(p,2);
                if ((pow(z,k)) <= (UPPER_LIMIT)) {
                   tempS->no=pow(z,k);
                   tempS->next=(PrimesList*)malloc(sizeof(PrimesList));
                   tempS=tempS->next;
                   tempS->next=NULL;
                }
                else  flagS=1;
                k=k+1;
          }
          temp=temp->next;
    }
    temp=list;
    tempS=listS;
       
       while (tempS != NULL) {
          printf("%d next: \n ",tempS->no);
          tempS = tempS->next;
       }

       system("pause");
       //getchar();
       return 0;
    }
it doesn't print me all the numbers..
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: code help

Post by Neo » Tue Apr 27, 2010 7:19 pm

The sequence looks like p(2+k) < UPPER_LIMIT where k = -1, 0, 1, 2, 3, ...... N

If you take the first prime number on the list.. i.e.: 2

k = -1, 2(2+(-1)) = 21 = 2 ? Save same number to the list
k = 0, 2(2+( 0)) = 22 = 4
k = 1, 2(2+( 1)) = 23 = 8
k = 2, 2(2+( 2)) = 24 = 16
k = 3, 2(2+( 3)) = 25 = 32 (We will not save this number as UPPER_LIMIT = 24)

So our list for 2 is, 2, 4, 8, 16

k = -1, 3(2+(-1)) = 31 = 3 ? Save same number to the list
k = 0, 3(2+( 0)) = 32 = 9
k = 1, 3(2+( 1)) = 33 = 27 (We will not save this number as UPPER_LIMIT = 24)

So the next list would be 3, 9

and so on for 5, 7, 11, 13, 17, 23

Can you confirm this?
ghostrider_gr
Sergeant
Sergeant
Posts: 17
Joined: Mon Apr 19, 2010 1:01 am

Re: code help

Post by ghostrider_gr » Wed Apr 28, 2010 1:22 am

Neo wrote:The sequence looks like p(2+k) < UPPER_LIMIT where k = -1, 0, 1, 2, 3, ...... N

If you take the first prime number on the list.. i.e.: 2

k = -1, 2(2+(-1)) = 21 = 2 ? Save same number to the list
k = 0, 2(2+( 0)) = 22 = 4
k = 1, 2(2+( 1)) = 23 = 8
k = 2, 2(2+( 2)) = 24 = 16
k = 3, 2(2+( 3)) = 25 = 32 (We will not save this number as UPPER_LIMIT = 24)

So our list for 2 is, 2, 4, 8, 16

k = -1, 3(2+(-1)) = 31 = 3 ? Save same number to the list
k = 0, 3(2+( 0)) = 32 = 9
k = 1, 3(2+( 1)) = 33 = 27 (We will not save this number as UPPER_LIMIT = 24)

So the next list would be 3, 9

and so on for 5, 7, 11, 13, 17, 23

Can you confirm this?
thanks for your help... i solved it.. i constructed power because i couldn;t handle pow.. and its ok now..

next one because this one drives me crazy.. :twisted:

this is part of my code...

Code: Select all

printf("%d\n",listS->no);//right print
       printf("%d\n",listS->next->no);//right print
       printf("%d",tempS->no);//right
       system("pause");


 printf("%d",listS->no);//false
i mean "listS->no" doesn't change..why it prints me false results.. it prints something irrelevent and then i get a message..".exe has stopped working"..
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: code help

Post by Neo » Wed Apr 28, 2010 1:39 am

It sounds like listS is NULL.
ghostrider_gr
Sergeant
Sergeant
Posts: 17
Joined: Mon Apr 19, 2010 1:01 am

Re: code help

Post by ghostrider_gr » Wed Apr 28, 2010 1:54 am

Neo wrote:It sounds like listS is NULL.
yes but in the first printf i get the right result... i mean its like my programm doesn't work from that point.. :cry:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: code help

Post by Neo » Wed Apr 28, 2010 1:59 am

It is not good to do something like list->next->no when you don't know that list->next is not NULL. This might corrupt the memory. So before printing, make sure, you check list->next != NULL.
Post Reply

Return to “C/C++ Programming”