Thread: Looking for some help

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    3

    Looking for some help

    Hello guys. My task is to find the first n natural numbers that have more divisors than any other natural number before them for a given n. So basicly if n=5, then the program have to print 1,2,4,6,12. I made it work but now my problem is that my program works only to n=28,29 because the numbers seems to get quite large. For some reason i cannot make my k bigger than 99999 because then something breaks and i cannot fix it. Is there a way to make this work for any given n, or at least for more than 29.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int FindDevisors(int n) 
    { 
        int i,j=0;
        for (i=1;i<=n;i++) 
        {    
            if (n%i==0) 
            {
                j++;
                
            }
        }
        
        return j;
    } 
      
    int main() 
    { 
        int n,i,j,k,br=0,brc;
        
        printf("Enter n ");
        scanf("%d", &n);
        
        k=99999;
        int answer[n];
        int numbers[k];
        //int *current;
        //current = (int*)malloc(n * sizeof(int)); 
        int current[k];
        
        
        for(i=0,j=0;i<=k,j<=n;i++)
        {
            current[i]=FindDevisors(i);
            
            if(br<current[i])
            {
                for(brc=0;brc<i;brc++)
                {
                    numbers[j]=i;
                }
                br=current[i];
                answer[j]=current[i];
                j++;
                
            }    
            //printf("Devisors of %d are : %d\n",i,current[i]);
        }
        
        for(i=0;i<n;i++)
        {
            printf("\n Answer %d = %d and have  %d devisors ",i+1,numbers[i], answer[i]);
        }
        
        return 0; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    Code:
        k=99999;
        int answer[n];
        int numbers[k];
        //int *current;
        //current = (int*)malloc(n * sizeof(int)); 
        int current[k];
    The stack isn't an infinite amount of space you can create monster sized arrays in.

    If you want larger arrays, you have to use malloc (which you've started to do).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2020
    Posts
    3
    Yeah i figured out i probably have to use dynamic memory, but i cannot manage to do it.With what i have tried it just doesnt work

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    There may be another alternative: declare the array with static storage duration. This could mean using the static keyword, or it could mean moving the array declaration to file scope. You then probably don't want it to be a variable length array, so #define K appropriately instead of declaring k as a variable in main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    > for(i=0,j=0;i<=k,j<=n;i++)
    Well one reason would be that you're accessing outside the bounds of the array.
    You should have <, not <= in your loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2020
    Posts
    3
    Code:
    	
            static int numbers[2147483647];
    	static int current[2147483647];
    I guess this worked as a solution for now, couldnt figure out how to do it with malloc but thanks anyways

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    > I guess this worked as a solution for now,
    If you call using 16GB of memory 'a solution'.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int FindDevisors(int n)
    {
        int i,j=0;
        for (i=1;i<=n;i++)
        {
            if (n%i==0)
            {
                j++;
    
            }
        }
    
        return j;
    }
    
    int main()
    {
        int n,i,j,k,br=0,brc;
    
        printf("Enter n ");
        scanf("%d", &n);
    
        k=999999;
        int answer[n];
        //int numbers[k];
        int *numbers = malloc(k * sizeof(int));
        int *current = (int*)malloc(k * sizeof(int));
        // int current[k];
    
    
        for(i=0,j=0;i<k,j<n;i++)
        {
            current[i]=FindDevisors(i);
    
            if(br<current[i])
            {
                for(brc=0;brc<i;brc++)
                {
                    numbers[j]=i;
                }
                br=current[i];
                answer[j]=current[i];
                j++;
    
            }
            //printf("Devisors of %d are : %d\n",i,current[i]);
        }
    
        for(i=0;i<n;i++)
        {
            printf("\n Answer %d = %d and have  %d devisors ",i+1,numbers[i], answer[i]);
        }
    
        free(numbers);
        free(current);
        return 0;
    }
    
    $ ./a.out
    Enter n 30
    
     Answer 1 = 1 and have  1 devisors 
     Answer 2 = 2 and have  2 devisors 
     Answer 3 = 4 and have  3 devisors 
     Answer 4 = 6 and have  4 devisors 
     Answer 5 = 12 and have  6 devisors 
     Answer 6 = 24 and have  8 devisors 
     Answer 7 = 36 and have  9 devisors 
     Answer 8 = 48 and have  10 devisors 
     Answer 9 = 60 and have  12 devisors 
     Answer 10 = 120 and have  16 devisors 
     Answer 11 = 180 and have  18 devisors 
     Answer 12 = 240 and have  20 devisors 
     Answer 13 = 360 and have  24 devisors 
     Answer 14 = 720 and have  30 devisors 
     Answer 15 = 840 and have  32 devisors 
     Answer 16 = 1260 and have  36 devisors 
     Answer 17 = 1680 and have  40 devisors 
     Answer 18 = 2520 and have  48 devisors 
     Answer 19 = 5040 and have  60 devisors 
     Answer 20 = 7560 and have  64 devisors 
     Answer 21 = 10080 and have  72 devisors 
     Answer 22 = 15120 and have  80 devisors 
     Answer 23 = 20160 and have  84 devisors 
     Answer 24 = 25200 and have  90 devisors 
     Answer 25 = 27720 and have  96 devisors 
     Answer 26 = 45360 and have  100 devisors 
     Answer 27 = 50400 and have  108 devisors 
     Answer 28 = 55440 and have  120 devisors 
     Answer 29 = 83160 and have  128 devisors 
     Answer 30 = 110880 and have  144 devisors $
    It starts to seriously slow down after about 20.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    945
    Code:
    `                         Prime Factors
    Ord       Value  Divs     2  3  5  7 11 13 17 19
      1:          1     1
      2:          2     2     1
      3:          4     3     2
      4:          6     4     1  1
      5:         12     6     2  1
      6:         24     8     3  1
      7:         36     9     2  2
      8:         48    10     4  1
      9:         60    12     2  1  1
     10:        120    16     3  1  1
     11:        180    18     2  2  1
     12:        240    20     4  1  1
     13:        360    24     3  2  1
     14:        720    30     4  2  1
     15:        840    32     3  1  1  1
     16:       1260    36     2  2  1  1
     17:       1680    40     4  1  1  1
     18:       2520    48     3  2  1  1
     19:       5040    60     4  2  1  1
     20:       7560    64     3  3  1  1
     21:      10080    72     5  2  1  1
     22:      15120    80     4  3  1  1
     23:      20160    84     6  2  1  1
     24:      25200    90     4  2  2  1
     25:      27720    96     3  2  1  1  1
     26:      45360   100     4  4  1  1
     27:      50400   108     5  2  2  1
     28:      55440   120     4  2  1  1  1
     29:      83160   128     3  3  1  1  1
     30:     110880   144     5  2  1  1  1
     31:     166320   160     4  3  1  1  1
     32:     221760   168     6  2  1  1  1
     33:     277200   180     4  2  2  1  1
     34:     332640   192     5  3  1  1  1
     35:     498960   200     4  4  1  1  1
     36:     554400   216     5  2  2  1  1
     37:     665280   224     6  3  1  1  1
     38:     720720   240     4  2  1  1  1  1
     39:    1081080   256     3  3  1  1  1  1
     40:    1441440   288     5  2  1  1  1  1
     41:    2162160   320     4  3  1  1  1  1
     42:    2882880   336     6  2  1  1  1  1
     43:    3603600   360     4  2  2  1  1  1
     44:    4324320   384     5  3  1  1  1  1
     45:    6486480   400     4  4  1  1  1  1
     46:    7207200   432     5  2  2  1  1  1
     47:    8648640   448     6  3  1  1  1  1
     48:   10810800   480     4  3  2  1  1  1
     49:   14414400   504     6  2  2  1  1  1
     50:   17297280   512     7  3  1  1  1  1
     51:   21621600   576     5  3  2  1  1  1
     52:   32432400   600     4  4  2  1  1  1
     53:   36756720   640     4  3  1  1  1  1
     54:   43243200   672     6  3  2  1  1  1
     55:   61261200   720     4  2  2  1  1  1  1
     56:   73513440   768     5  3  1  1  1  1  1
     57:  110270160   800     4  4  1  1  1  1  1
     58:  122522400   864     5  2  2  1  1  1  1
     59:  147026880   896     6  3  1  1  1  1  1
     60:  183783600   960     4  3  2  1  1  1  1
     61:  245044800  1008     6  2  2  1  1  1  1
     62:  294053760  1024     7  3  1  1  1  1  1
     63:  367567200  1152     5  3  2  1  1  1  1
     64:  551350800  1200     4  4  2  1  1  1  1
     65:  698377680  1280     4  3  1  1  1  1  1  1
     66:  735134400  1344     6  3  2  1  1  1  1
     67: 1102701600  1440     5  4  2  1  1  1  1
     68: 1396755360  1536     5  3  1  1  1  1  1  1
     69: 2095133040  1600     4  4  1  1  1  1  1  1
     70: 2205403200  1680     6  4  2  1  1  1  1
     71: 2327925600  1728     5  2  2  1  1  1  1  1
     72: 2793510720  1792     6  3  1  1  1  1  1  1
     73: 3491888400  1920     4  3  2  1  1  1  1  1
     74: 4655851200  2016     6  2  2  1  1  1  1  1
     75: 5587021440  2048     7  3  1  1  1  1  1  1
    Last edited by john.c; 06-01-2020 at 07:48 PM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    945
    Code:
    `                                        2  3  5  7 11 13 17 19 23 29 31 37 41
      1:                    1       1        
      2:                    2       2        1
      3:                    4       3        2
      4:                    6       4        1  1
      5:                   12       6        2  1
      6:                   24       8        3  1
      7:                   36       9        2  2
      8:                   48      10        4  1
      9:                   60      12        2  1  1
     10:                  120      16        3  1  1
     11:                  180      18        2  2  1
     12:                  240      20        4  1  1
     13:                  360      24        3  2  1
     14:                  720      30        4  2  1
     15:                  840      32        3  1  1  1
     16:                 1260      36        2  2  1  1
     17:                 1680      40        4  1  1  1
     18:                 2520      48        3  2  1  1
     19:                 5040      60        4  2  1  1
     20:                 7560      64        3  3  1  1
     21:                10080      72        5  2  1  1
     22:                15120      80        4  3  1  1
     23:                20160      84        6  2  1  1
     24:                25200      90        4  2  2  1
     25:                27720      96        3  2  1  1  1
     26:                45360     100        4  4  1  1
     27:                50400     108        5  2  2  1
     28:                55440     120        4  2  1  1  1
     29:                83160     128        3  3  1  1  1
     30:               110880     144        5  2  1  1  1
     31:               166320     160        4  3  1  1  1
     32:               221760     168        6  2  1  1  1
     33:               277200     180        4  2  2  1  1
     34:               332640     192        5  3  1  1  1
     35:               498960     200        4  4  1  1  1
     36:               554400     216        5  2  2  1  1
     37:               665280     224        6  3  1  1  1
     38:               720720     240        4  2  1  1  1  1
     39:              1081080     256        3  3  1  1  1  1
     40:              1441440     288        5  2  1  1  1  1
     41:              2162160     320        4  3  1  1  1  1
     42:              2882880     336        6  2  1  1  1  1
     43:              3603600     360        4  2  2  1  1  1
     44:              4324320     384        5  3  1  1  1  1
     45:              6486480     400        4  4  1  1  1  1
     46:              7207200     432        5  2  2  1  1  1
     47:              8648640     448        6  3  1  1  1  1
     48:             10810800     480        4  3  2  1  1  1
     49:             14414400     504        6  2  2  1  1  1
     50:             17297280     512        7  3  1  1  1  1
     51:             21621600     576        5  3  2  1  1  1
     52:             32432400     600        4  4  2  1  1  1
     53:             36756720     640        4  3  1  1  1  1  1
     54:             43243200     672        6  3  2  1  1  1
     55:             61261200     720        4  2  2  1  1  1  1
     56:             73513440     768        5  3  1  1  1  1  1
     57:            110270160     800        4  4  1  1  1  1  1
     58:            122522400     864        5  2  2  1  1  1  1
     59:            147026880     896        6  3  1  1  1  1  1
     60:            183783600     960        4  3  2  1  1  1  1
     61:            245044800    1008        6  2  2  1  1  1  1
     62:            294053760    1024        7  3  1  1  1  1  1
     63:            367567200    1152        5  3  2  1  1  1  1
     64:            551350800    1200        4  4  2  1  1  1  1
     65:            698377680    1280        4  3  1  1  1  1  1  1
     66:            735134400    1344        6  3  2  1  1  1  1
     67:           1102701600    1440        5  4  2  1  1  1  1
     68:           1396755360    1536        5  3  1  1  1  1  1  1
     69:           2095133040    1600        4  4  1  1  1  1  1  1
     70:           2205403200    1680        6  4  2  1  1  1  1
     71:           2327925600    1728        5  2  2  1  1  1  1  1
     72:           2793510720    1792        6  3  1  1  1  1  1  1
     73:           3491888400    1920        4  3  2  1  1  1  1  1
     74:           4655851200    2016        6  2  2  1  1  1  1  1
     75:           5587021440    2048        7  3  1  1  1  1  1  1
     76:           6983776800    2304        5  3  2  1  1  1  1  1
     77:          10475665200    2400        4  4  2  1  1  1  1  1
     78:          13967553600    2688        6  3  2  1  1  1  1  1
     79:          20951330400    2880        5  4  2  1  1  1  1  1
     80:          27935107200    3072        7  3  2  1  1  1  1  1
     81:          41902660800    3360        6  4  2  1  1  1  1  1
     82:          48886437600    3456        5  3  2  2  1  1  1  1
     83:          64250746560    3584        6  3  1  1  1  1  1  1  1
     84:          73329656400    3600        4  4  2  2  1  1  1  1
     85:          80313433200    3840        4  3  2  1  1  1  1  1  1
     86:          97772875200    4032        6  3  2  2  1  1  1  1
     87:         139675536000    4096        7  3  3  1  1  1  1  1
     88:         146659312800    4320        5  4  2  2  1  1  1  1
     89:         160626866400    4608        5  3  2  1  1  1  1  1  1
     90:         240940299600    4800        4  4  2  1  1  1  1  1  1
     91:         293318625600    5040        6  4  2  2  1  1  1  1
     92:         321253732800    5376        6  3  2  1  1  1  1  1  1
     93:         481880599200    5760        5  4  2  1  1  1  1  1  1
     94:         642507465600    6144        7  3  2  1  1  1  1  1  1
     95:         963761198400    6720        6  4  2  1  1  1  1  1  1
     96:        1124388064800    6912        5  3  2  2  1  1  1  1  1
     97:        1606268664000    7168        6  3  3  1  1  1  1  1  1
     98:        1686582097200    7200        4  4  2  2  1  1  1  1  1
     99:        1927522396800    7680        7  4  2  1  1  1  1  1  1
    100:        2248776129600    8064        6  3  2  2  1  1  1  1  1
    101:        3212537328000    8192        7  3  3  1  1  1  1  1  1
    102:        3373164194400    8640        5  4  2  2  1  1  1  1  1
    103:        4497552259200    9216        7  3  2  2  1  1  1  1  1
    104:        6746328388800   10080        6  4  2  2  1  1  1  1  1
    105:        8995104518400   10368        8  3  2  2  1  1  1  1  1
    106:        9316358251200   10752        6  3  2  1  1  1  1  1  1  1
    107:       13492656777600   11520        7  4  2  2  1  1  1  1  1
    108:       18632716502400   12288        7  3  2  1  1  1  1  1  1  1
    109:       26985313555200   12960        8  4  2  2  1  1  1  1  1
    110:       27949074753600   13440        6  4  2  1  1  1  1  1  1  1
    111:       32607253879200   13824        5  3  2  2  1  1  1  1  1  1
    112:       46581791256000   14336        6  3  3  1  1  1  1  1  1  1
    113:       48910880818800   14400        4  4  2  2  1  1  1  1  1  1
    114:       55898149507200   15360        7  4  2  1  1  1  1  1  1  1
    115:       65214507758400   16128        6  3  2  2  1  1  1  1  1  1
    116:       93163582512000   16384        7  3  3  1  1  1  1  1  1  1
    117:       97821761637600   17280        5  4  2  2  1  1  1  1  1  1
    118:      130429015516800   18432        7  3  2  2  1  1  1  1  1  1
    119:      195643523275200   20160        6  4  2  2  1  1  1  1  1  1
    120:      260858031033600   20736        8  3  2  2  1  1  1  1  1  1
    121:      288807105787200   21504        6  3  2  1  1  1  1  1  1  1  1
    122:      391287046550400   23040        7  4  2  2  1  1  1  1  1  1
    123:      577614211574400   24576        7  3  2  1  1  1  1  1  1  1  1
    124:      782574093100800   25920        8  4  2  2  1  1  1  1  1  1
    125:      866421317361600   26880        6  4  2  1  1  1  1  1  1  1  1
    126:     1010824870255200   27648        5  3  2  2  1  1  1  1  1  1  1
    127:     1444035528936000   28672        6  3  3  1  1  1  1  1  1  1  1
    128:     1516237305382800   28800        4  4  2  2  1  1  1  1  1  1  1
    129:     1732842634723200   30720        7  4  2  1  1  1  1  1  1  1  1
    130:     2021649740510400   32256        6  3  2  2  1  1  1  1  1  1  1
    131:     2888071057872000   32768        7  3  3  1  1  1  1  1  1  1  1
    132:     3032474610765600   34560        5  4  2  2  1  1  1  1  1  1  1
    133:     4043299481020800   36864        7  3  2  2  1  1  1  1  1  1  1
    134:     6064949221531200   40320        6  4  2  2  1  1  1  1  1  1  1
    135:     8086598962041600   41472        8  3  2  2  1  1  1  1  1  1  1
    136:    10108248702552000   43008        6  3  3  2  1  1  1  1  1  1  1
    137:    12129898443062400   46080        7  4  2  2  1  1  1  1  1  1  1
    138:    18194847664593600   48384        6  5  2  2  1  1  1  1  1  1  1
    139:    20216497405104000   49152        7  3  3  2  1  1  1  1  1  1  1
    140:    24259796886124800   51840        8  4  2  2  1  1  1  1  1  1  1
    141:    30324746107656000   53760        6  4  3  2  1  1  1  1  1  1  1
    142:    36389695329187200   55296        7  5  2  2  1  1  1  1  1  1  1
    143:    48519593772249600   57600        9  4  2  2  1  1  1  1  1  1  1
    144:    60649492215312000   61440        7  4  3  2  1  1  1  1  1  1  1
    145:    72779390658374400   62208        8  5  2  2  1  1  1  1  1  1  1
    146:    74801040398884800   64512        6  3  2  2  1  1  1  1  1  1  1  1
    147:   106858629141264000   65536        7  3  3  1  1  1  1  1  1  1  1  1
    148:   112201560598327200   69120        5  4  2  2  1  1  1  1  1  1  1  1
    149:   149602080797769600   73728        7  3  2  2  1  1  1  1  1  1  1  1
    150:   224403121196654400   80640        6  4  2  2  1  1  1  1  1  1  1  1
    151:   299204161595539200   82944        8  3  2  2  1  1  1  1  1  1  1  1
    152:   374005201994424000   86016        6  3  3  2  1  1  1  1  1  1  1  1
    153:   448806242393308800   92160        7  4  2  2  1  1  1  1  1  1  1  1
    154:   673209363589963200   96768        6  5  2  2  1  1  1  1  1  1  1  1
    155:   748010403988848000   98304        7  3  3  2  1  1  1  1  1  1  1  1
    156:   897612484786617600  103680        8  4  2  2  1  1  1  1  1  1  1  1
    157:  1122015605983272000  107520        6  4  3  2  1  1  1  1  1  1  1  1
    158:  1346418727179926400  110592        7  5  2  2  1  1  1  1  1  1  1  1
    159:  1795224969573235200  115200        9  4  2  2  1  1  1  1  1  1  1  1
    160:  2244031211966544000  122880        7  4  3  2  1  1  1  1  1  1  1  1
    161:  2692837454359852800  124416        8  5  2  2  1  1  1  1  1  1  1  1
    162:  3066842656354276800  129024        6  3  2  2  1  1  1  1  1  1  1  1  1
    163:  4381203794791824000  131072        7  3  3  1  1  1  1  1  1  1  1  1  1
    164:  4488062423933088000  138240        8  4  3  2  1  1  1  1  1  1  1  1
    165:  6133685312708553600  147456        7  3  2  2  1  1  1  1  1  1  1  1  1
    166:  8976124847866176000  153600        9  4  3  2  1  1  1  1  1  1  1  1
    167:  9200527969062830400  161280        6  4  2  2  1  1  1  1  1  1  1  1  1
    168: 12267370625417107200  165888        8  3  2  2  1  1  1  1  1  1  1  1  1
    169: 15334213281771384000  172032        6  3  3  2  1  1  1  1  1  1  1  1  1
    170: 18401055938125660800  184320        7  4  2  2  1  1  1  1  1  1  1  1  1
                                             2  3  5  7 11 13 17 19 23 29 31 37 41
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Tags for this Thread