Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Hi all !
I'm a newbie ... i have a question as follows:

Compute sum (in decimal) of consecutive sequence
Example:

n=12 => 1 + 2 + 3 + 4 +5 +6 +7 +8 +9 + (1+0) + (1+1) + (1+2)=51
n= 123 ?
n= 13579 ?
n= 123123123123?
n= 98765432100123456789 ?

Welcome to see all solutions that you think is the best !

Hope to see any aid !


-----------------------------------------------------
Don't make me fail if you think this is my homework :D
Posted
Updated 12-Mar-11 1:41am
v2
Comments
Abhinav S 12-Mar-11 6:12am    
Homework!
Sergey Alexandrovich Kryukov 12-Mar-11 19:22pm    
Why not letting you fail? Any rational reason?
--SA

Nice exercise for saturday night, thanks:

C#
#include <stdio.h>
#include <tchar.h>
double Dix(const TCHAR* anum,const unsigned int nnum,const unsigned int ix)
{
  return ix<nnum?(double)(anum[ix]-'0'):0.0;
}
double HornerSchema(const TCHAR* anum,const unsigned int nnum,const unsigned int lo,const unsigned int hi)
{
  double        res = 0.0;
  unsigned int  ix;
  for(ix=hi;lo<ix;ix--) res = res*10.0 + Dix(anum,nnum,nnum-ix);
  for(;0<ix;ix--) res *= 10.0;
  return res;
}
double Upper(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return HornerSchema(anum,nnum,pos+1,nnum);
}
double Lower(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return HornerSchema(anum,nnum,0,pos);
}
double Pow10(const unsigned int pos)
{
  double        res = 1.0;
  unsigned int  ix;
  for(ix=0;ix<pos;ix++) res*=10.0;
  return res;
}
unsigned int Digit(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return pos<nnum?(unsigned int)(anum[nnum-pos-1]-'0'):0;
}
double HSum(const TCHAR* anum,const unsigned int nnum)
{
  double        sum = 0.0;
  unsigned int  i,di;
  double        hi,lo;
  double        hisum,disum,llsum;
  double        ASUM[] = { 0,0,1,3,6,10,15,21,28,36,45 };
  for(i=0;i<nnum;i++)
  {
    hi    = Upper(anum,nnum,i);
    di    = Digit(anum,nnum,i);
    lo    = Lower(anum,nnum,i);
    hisum = hi*4.5;
    disum = Pow10(i)*ASUM[di];
    llsum = di*(lo+1.0);
    sum  += hisum+disum+llsum;
  }
  return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
  const TCHAR  number[] = __T("98765432100123456789");
  double      sum;
  sum = HSum(number,(sizeof(number)/sizeof(number[0]))-1);
  if(1e15<sum) _tprintf(__T("%s = ~ %.15G"),number,sum); // double is limited to about 15 digits
  else _tprintf(__T("%s = %.0lf"),number,sum);
  _gettch();
  return 0;
}


VB
123 = 1038
13579 = 235360
123123123123 = 6018595171770
9,87654321001235E+19 = 8,87700045735992E+21


Regards.
 
Share this answer
 
Comments
mbue 12-Mar-11 19:37pm    
You voter of 4! Do you think you have a faster and smaller solution? Thanks for your anonymous devaluation.
Regards.
This seems to be homework, you should do it by yourself...

Anyway you should do it easily: simply divide the problems and then the solution will appear automatically.

As a newbie you should only not to worry on how to implement the solution rather than thinking on a pseudocode solution, start identifying the problems, then make a flowchart identifying the involved routines and variables, then use pseudocode to implement it and at the end implement the functions and then the program itself.

You'll get the result without much effort.

This is the best advice you'll get here with a question like that, apart of that you'll get posts like "this is homework", "we won't help you", ...

MAke all the steps and if you get stuck, send a specific question and somebody will help you answering it.

Good luck!
 
Share this answer
 
Comments
bluepen 12-Mar-11 7:35am    
Thanks ! But this isn't my homework :D ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900