Calculating S in C task

This is a discussion on Calculating S in C task within the C Programming forums, part of the General Programming Boards category; Hello guys. Im just starting out with programing in C and i got a task that i dont know how ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Calculating S in C task

    Hello guys.

    Im just starting out with programing in C and i got a task that i dont know how to do.

    The task is as follows :

    Write a program in C that will calculate S for the input value of n:

    http://i23.photobucket.com/albums/b3...pressorX/1.jpg

    The problem im having is how do i, if the value for n is for example 5, do the calculations for all the numbers <=5 (1,2,3,4,5). That is how do i write a code that will for any value given do the calculations on all the numbers from 1 to the value given calculate the factorials for each one and then calculate S using the formula on the image.

    Seems to be a hell of a task for someone who is just starting with C :\
    Anyway if anyone could help clarify this i would be very grateful.

    Cheers.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You write a loop to calculate the top, you write a loop to calculate the bottom, divide, and you're done.

    Note that you don't have to calculate the factorial from scratch each time, since the value of n! depends on (n-1)! which you just calculated.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, note that for sufficiently large n values, the value of the numerator cannot be held in any known type, not even long int. So don't expect your program to run for say, n = 1000.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    I think mine breaks down well before it even reaches 100 using long.


    [cd@localhost oakland]$ more task.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned long fact(unsigned long);
    unsigned long sum_fact(unsigned long);
    float harmonic(unsigned long);
    
    unsigned long fact(unsigned long n)
    {
      unsigned long acc = 1;
      unsigned long i;
      
      for (i = 1; i <= n; i++)
        acc *= i;
    
      return acc;
    }
    
    unsigned long sum_fact(unsigned long n)
    {
      unsigned long acc = 0;
      unsigned long i;
    
      for (i = 1; i <= n; i++) {
        acc += fact(i);
      }
    
      return acc;
    }
    
    float harmonic(unsigned long n)
    {
      float acc = 0;
      float i;
    
      for (i = 1; i <= n; i++) {
        acc += ((100) *1/i);
      }
    
      return acc;
    }
    
    int main(int argc, char **argv)
    {
      unsigned long s;
    
      if (argc != 2) {
        fprintf(stderr, "Invalid number of args\n");
        return 1;
      }
    
      s = atoi(argv[1]);  
    
      printf("The final value is: %f\n", sum_fact(s)/(harmonic(s)/100));
    
      return 0;
    }


    Does your version produce different results?
    I didn't actually know the exact value when you exceed long with factorial calculations so I just provided a safe example. I am sure it is much lower than 1000 like I said.

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Actually, I get marginally better results when I change the floats to doubles.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Hello again guys

    Thank you all for your reply's.

    Im trying to learn and the code you wrote is a bit over my head

    Could someone add some comments into that code to better explain it or write a simpler code please ? Yeah yeah i know im a total noob

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    That code I wrote only produces the correct results up to like the number 6. Does your professor or teacher want the code to work beyond that?

  8. #8
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by claudiu View Post
    Also, note that for sufficiently large n values, the value of the numerator cannot be held in any known type, not even long int. So don't expect your program to run for say, n = 1000.
    That statement is what caused my program to bork after the first 6 values. So I disregarded that bad advice, and now, as it stands, I can get this program to produce accurate results up to around 1e300.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    That statement is what caused my program to bork after the first 6 values. So I disregarded that bad advice, and now, as it stands, I can get this program to produce accurate results up to around 1e300.
    Well perhaps you are using double values, however that is weird, because mathematically the factorial function makes no sense for non-integer values.

    Also, in my experience factorial(14) exceeds long int on 32bit systems.

    Also, that wasn't advice meant for you, and it wasn't bad advice. I wasn't advising anything in fact, just warning the OP not to be surprised if the cannot compute the sum for large values of n.

  10. #10
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    You have no clue what you are talking about buddy. Here is what I get on my 32 bit PC. Please note that I'm not using any kind of multiprecision library.

    [cd@localhost oakland]$ ./task2
    The factorial of i 0 is: 1
    The factorial of i 1 is: 1
    The factorial of i 2 is: 2
    The factorial of i 3 is: 6
    The factorial of i 4 is: 24
    The factorial of i 5 is: 120
    The factorial of i 6 is: 720
    The factorial of i 7 is: 5040
    The factorial of i 8 is: 40320
    The factorial of i 9 is: 362880
    The factorial of i 10 is: 3628800
    The factorial of i 11 is: 39916800
    The factorial of i 12 is: 479001600
    The factorial of i 13 is: 6227020800
    The factorial of i 14 is: 87178291200
    The factorial of i 15 is: 1307674368000
    The factorial of i 16 is: 20922789888000
    The factorial of i 17 is: 355687428096000
    The factorial of i 18 is: 6402373705728000
    The factorial of i 19 is: 121645100408832000
    The factorial of i 20 is: 2432902008176640000
    The factorial of i 21 is: 51090942171709440000
    The factorial of i 22 is: 1124000727777607680000
    The factorial of i 23 is: 25852016738884978212864
    The factorial of i 24 is: 620448401733239409999872
    The factorial of i 25 is: 15511210043330986055303168
    The factorial of i 26 is: 403291461126605650322784256
    The factorial of i 27 is: 10888869450418351940239884288
    The factorial of i 28 is: 304888344611713836734530715648
    The factorial of i 29 is: 8841761993739700772720181510144
    The factorial of i 30 is: 265252859812191032188804700045312
    The factorial of i 31 is: 8222838654177922430198509928972288
    The factorial of i 32 is: 263130836933693517766352317727113216
    The factorial of i 33 is: 8683317618811885938715673895318323200
    The factorial of i 34 is: 295232799039604119555149671006000381952
    The factorial of i 35 is: 10333147966386144222209170348167175077888
    The factorial of i 36 is: 371993326789901177492420297158468206329856
    The factorial of i 37 is: 13763753091226343102992036262845720547033088
    The factorial of i 38 is: 523022617466601037913697377988137380787257344
    The factorial of i 39 is: 20397882081197441587828472941238084160318341120
    The factorial of i 40 is: 815915283247897683795548521301193790359984930816
    The factorial of i 41 is: 33452526613163802763987613764361857922667238129664
    The factorial of i 42 is: 14050061177528797887796357975907848321789726105272 32
    The factorial of i 43 is: 60415263063373834074440829285578945930237590418489 344
    The factorial of i 44 is: 26582715747884485291342130280962418892431502625294 25408
    The factorial of i 45 is: 11962222086548018857499272315746937350318626585857 9103744
    The factorial of i 46 is: 55026221598120884566689504358429745645868194731629 83440384
    The factorial of i 47 is: 25862324151116817767349100665299702655232519982623 7836492800
    The factorial of i 48 is: 12413915592536072528327568319343857274511609591659 416151654400
    The factorial of i 49 is: 60828186403426752248860160811673162316877754210241 8391010639872
    The factorial of i 50 is: 30414093201713375576366966406747986832057064836514 787179557289984
    The factorial of i 51 is: 15511187532873821894707545826858173653233462918530 46617899802820608
    The factorial of i 52 is: 80658175170943876845634591553351679477960544579306 048386139594686464
    The factorial of i 53 is: 42748832840600254847912547653423957182564950123150 11061486797910441984
    The factorial of i 54 is: 23084369733924137924371883906026708550254478496562 8964557765331531071488
    The factorial of i 55 is: 12696403353658276446882823840816011312245221598828 319560272916152712167424
    The factorial of i 56 is: 71099858780486348102543813508569663348573240953438 5895375283304551881375744
    The factorial of i 57 is: 40526919504877220527556156789809444757511993541235 911846782577699372834750464
    The factorial of i 58 is: 23505613312828789062977962804562476349569662739553 90268712005058924708557225984
    The factorial of i 59 is: 13868311854568986493322118514385335285353380986813 3429504739525869642019130834944
    The factorial of i 60 is: 83209871127413915800563961029596410774579455410767 08813599085350531187384917164032
    The factorial of i 61 is: 50758021387722483583354016137308849072428138984387 1724559898414118829028410677788672
    The factorial of i 62 is: 31469973260387939390320343330721249710233204778005 956144519390914718240063804258910208
    The factorial of i 63 is: 19826083154044400849657327747675457076581098291360 18902789196017241837351744329178152960
    The factorial of i 64 is: 12688693218588416543780689758512292529011902906470 5209778508545103477590511637067401789440
    The factorial of i 65 is: 82476505920824715167353803272950205238422572101466 37473076098881993433291790288339528056832
    The factorial of i 66 is: 54434493907744306944549606027563585676128303456871 8387417404234993819829995466026946857533440
    The factorial of i 67 is: 36471110918188683221214362054827498508015278133658 067038296405766134083781086959639263732301824
    The factorial of i 68 is: 24800355424368305479709011539871079838475553997610 61789915503815309070879417337773547217359994880
    The factorial of i 69 is: 17112245242814129737573543427207344887665272148062 8511030304905066123383956194496253690059725733888
    The factorial of i 70 is: 11978571669969890269925854460558840225267029209529 30327894441987121439652486137449869147396683648204 8
    The factorial of i 71 is: 85047858856786217613936449886228345036310665787675 91198841375469698502919843466238457218031568457564 16
    The factorial of i 72 is: 61234458376886076682034243918084408426143679367126 65663165790338182922102287295691689196982729289446 1952
    The factorial of i 73 is: 44701154615126833670305181118791598550111254536753 76127499372976904192242294440576507107183576895048 384512
    The factorial of i 74 is: 33078854415193855897507845860662739792859484152508 70281776116319612289727490863558896194327680067026 63467008
    The factorial of i 75 is: 24809140811395391401649674453868616759922516881580 71788414496361861249279332952165699984471818630591 6810297344
    The factorial of i 76 is: 18854947016660498466497675672866749860207537596978 89931196791720482648043560619012598537844549685032 003930423296
    The factorial of i 77 is: 14518309202828583792503372319096021362422032622554 35103768148971825823031107874629923099483077456787 11432381726720
    The factorial of i 78 is: 11324281178206294606285193764734547659641544873910 04946923957011069964462128277615997883249321868933 1409071173009408
    The factorial of i 79 is: 89461821307829729139453610567812466009169986197986 48308952814858909714164875041679179517605592838429 69422038852173824
    The factorial of i 80 is: 71569457046263778832073404098641551692451427821500 63022833152440197864351902213150585239848442081667 5798776564959674368
    The factorial of i 81 is: 57971260207473655478592076093169551533024183171149 24299299934140244381749043408420663995193999459259 329102516576025837568
    The factorial of i 82 is: 47536433370128398180495087193420485740326098790968 46149322895670048826746343260086552162341734100834 75042065689611178344448
    The factorial of i 83 is: 39455239697206569095363763848575524105091557652834 96303506852064709033876219504437113383877459427362 1857211161281310377377792
    The factorial of i 84 is: 33142401345653519918939627851870022559861385859850 99085000359647021178112607661449751964466234594461 331925608329126314254532608
    The factorial of i 85 is: 28171041143805493614534007006373176927061869759470 12344539230805716369728015129071717912716514538519 61008097129997800044545179648
    The factorial of i 86 is: 24227095383672724277628115968482030522825707406365 31902389828764127730309092057835127177335715856284 2391230015963508220979549569024
    The factorial of i 87 is: 21077572983795269087233798237224287232533562814767 96516744835439819327865545820130415624631892617112 325588092315921658107822337949696
    The factorial of i 88 is: 18548264225739835535901544164134037971700252072400 16758480289521551579184245782821179440007274429102 32086550560628100855367408841392128
    The factorial of i 89 is: 16507955160908452497218052643056785820348586593118 45413122829237902376787637399862196368143237439636 0185860939835599722920061648806871040
    The factorial of i 90 is: 14857159644817606885981264446583904686485043853384 94631194665214503009929244428884407664620677918947 601692538663274334960161140774153486336
    The factorial of i 91 is: 13520015276784022811614124898346447400031519033616 40900230531689750939626595955894949452581812592272 89565007146009064474002163016890673266688
    The factorial of i 92 is: 12438414054641300055918190849808704283732243800785 47220372709793200068781189729048717160969194947398 8773572358313712866199772466321053448142848
    The factorial of i 93 is: 11567725070816408727081687710539103627025574530462 86765300873130615713476227390429979366250504125810 956242157790289615727775585913909863306493952
    The factorial of i 94 is: 10873661566567424099481672918376200175213508153269 45671496181710119458510764448576875774371202782175 89896360543234882091889200965381187714945122304
    The factorial of i 95 is: 10329978488239052206885505130495304991006115078121 22542697733155293234520998332548789377219063127615 7126501537780698430576453773283127019334195478528
    The factorial of i 96 is: 99167793487094910271584902947841059780206303350020 44351949939149962626753446741444177131444753429268 58025486847392744430654763018452435547907969515520
    The factorial of i 97 is: 96192759682482062236598631563798937437476306361515 29586027304906731941922694319282787888690071034057 90374215104335306490109904065237083146124714143907 84
    The factorial of i 98 is: 94268904488832420294101483608740343761375924661800 63696357188182614769297217373775790759257383412737 43132643950118370976987498563777033321270044226328 9856
    The factorial of i 99 is: 93326215443944096091160468772652940323762165415182 63059393616300788621604245200038032851664809578610 05701317510617187267217623578139262988057343784065 695744
    The factorial of i 100 is: 93326215443944102188325606108575267240944254854960 57150916691040040799506424293714863269403045051289 80429892969444748982587372043112366414775618770165 01813248
    [cd@localhost oakland]$

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No, you have no idea what you are talking about.

    Please, enlighten me how you store those values in long ints. I'm dying to know.

    Read and learn:

    limits.h - Wikipedia, the free encyclopedia

    And do us all a favor, stop arguing for the sake of arguing.

  12. #12
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by claudiu View Post
    No, you have no idea what you are talking about.

    Please, enlighten me how you store those values in long ints. I'm dying to know.

    Read and learn:

    limits.h - Wikipedia, the free encyclopedia

    And do us all a favor, stop arguing for the sake of arguing.
    You made a bad statement and now you are getting your panties in a bundle. Besides, I don't need Wikipedia to help me produce large values. Remember, I had my computer produce the large results using standard C. You didn't. So it looks like you are the one that doesn't know what they are talking about.

    And going slightly off topic, if you are using Linux, and have something like GMP, you can produce accurate results up to 3 billion.
    Last edited by Overworked_PhD; 04-20-2010 at 06:39 PM.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    You made a bad statement and now you are getting your panties in a bundle. Besides, I don't need Wikipedia to help me produce large values.

    And going slightly off topic, if you are using LInux, and have something like GMP, you can actually accurate results up to 3 blillion.
    <hands in ears> lalalaalalalalalalalalalalalala

    Who cares? We were talking about something completely different!

    you have been offtopic all day dude. I was talking about the maximum value held by a long, and you were talking about another way to produce large values.

  14. #14
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Code and results speak louder that name calling. Just remember that before you fire off another moronic statement.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    Code and results speak louder that name calling. Just remember that before you fire off another moronic statement.
    That's the smartest thing you said all day. The next thing is for you to learn how to apply it. I didn't call you any names. You just don't have enough friends obviously.

Page 1 of 9 123456789 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-31-2007, 10:40 AM
  2. Where do a task get "wakeuped", blocked, preempted?
    By micke_b in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2007, 03:49 PM
  3. A Task Buffer for storing socket descriptors
    By cloudy in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-09-2006, 01:08 PM
  4. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  5. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21