Google Answers Logo
View Question
 
Q: C++ Algorithm for Insipid Numbers ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: C++ Algorithm for Insipid Numbers
Category: Computers > Algorithms
Asked by: stonewallwoman-ga
List Price: $5.00
Posted: 10 Mar 2003 00:13 PST
Expires: 09 Apr 2003 01:13 PDT
Question ID: 174080
I need an algorithm in C++ to generate insipid numbers. I know what
the "word" equation says, but I can't seem to get it written correctly
in code.  Been beating my head against this keyboard for 4 days trying
to get it figured out on my own!

Request for Question Clarification by secret901-ga on 10 Mar 2003 01:26 PST
Hi stonewallwoman,
Could you clarify what "insipid numbers" are?  I've tried to research
what they are before attempting this, but couldn't find anything
helpful :-(
secret901-ga

Request for Question Clarification by secret901-ga on 10 Mar 2003 01:36 PST
I think I found it: by "insipid numbers," do you mean those numbers
that return a 1 after repeated additions of the squares of its digits?

secret901-ga

Clarification of Question by stonewallwoman-ga on 10 Mar 2003 08:17 PST
Yes, secret901, your second "clarification request" is correct.
Seperate the digits, square them, add them, and keep doing that in a
loop to get either a repeating sequence the ends in 1 or 58. I got a
long email from another source, but to tell you the truth, that guy
went WAY over my head! Hope you can help me in SIMPLE terms! :^)
Answer  
Subject: Re: C++ Algorithm for Insipid Numbers
Answered By: maniac-ga on 10 Mar 2003 17:58 PST
Rated:5 out of 5 stars
 
Hello Stonewallwoman,

Here is a c++ program that should do what you want. I'll first provide
some comments first to describe how the code works.

The function is_insipid is a little bit tricky but runs pretty simply.
The variable i will generate the sum. In each pass through the loop,
 i = i + (x%10)*(x%10)
basically a running sum of the squares of the digits. Note that x%10
is the remainder of x/10 - a digit. x will be divided by 10 each time
through the loop until it is less than 10. Note that integer x/10 is
zero - that will cause the loop to exit. The function then prints the
new sum and returns that sum to the caller.

The main program is pretty simple. Ask the user for a value, copy it
and pass it to is_insipid to compute the next value. Repeat the loop
until the result is 1 or 58. At that point, print the appropriate
answer and exit the program.

I tested it with the values from the web site
  http://www.stonehill.edu/compsci/CS103/Assignments/assignment3D.htm
(scroll down to the second assignment) and this gets the same answers.

If this is not clear - don't hesitate to use a clarification request
to get more information.

  --Maniac


#include <iostream.h>

int is_insipid(int x)

{
  int i = 0;

  while (x>0)
    {
      i += (x % 10)*(x % 10);
      x = x/10;
    }
  cout << "Now have " << i << "\n";
  return i;
}

int main(int argc,
     char *argv[])

{
  int x;
  int y;

  cout << "Enter a value:";
  cin >> x;
  y = x;
  do
    {
      y = is_insipid(y);
    }
  while ((y != 1) && (y != 58));
  if (y == 1)
    cout << x << " is insipid.\n";
  else
    cout << x << " is NOT insipid.\n";
  return 0;
}

  --Maniac
stonewallwoman-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
The answer I received from maniac-ga was consise, easy for my beginner
brain to comprhend, and eaisily translated into terms that my
instructor would accept.

Comments  
Subject: Re: C++ Algorithm for Insipid Numbers
From: eadfrith-ga on 10 Mar 2003 12:10 PST
 
This is an answer to the problem posed here:

http://www.stonehill.edu/compsci/CS103/Assignments/assignment3D.htm

It's in Java, since that's the language I know best. Shouldn't be too
hard to convert it to C++. Also, you can either tackle this using 
recursion or iteration. I've supplied solutions using both techniques.

Cheers.
 

  public static void main(String[] args)
  {
    int n = Integer.parseInt(args[0]);
    System.out.print("The sequence starting at " + n + " is: ");
    if(isInsipidIterative(n))
    {
      System.out.println("\nInspid");
    }
    else
    {
      System.out.println("\nNot Inspid");
    }
  }

  public static boolean isInsipidIterative(int n)
  {
    while(true)
    {
      System.out.print(n + " ");
      if(n == 1)
      {
        return true;
      }
      else if(n == 58)
      {
        return false;
      }
      else
      {
        n = sumDigits(n);
      }
    }
  }

  public static boolean isInsipidRecursive(int n)
  {
    System.out.print(n + " ");
    if(n == 1)
    {
      return true;
    }
    else if(n == 58)
    {
      return false;
    }
    else
    {
      return isInsipidRecursive(sumDigits(n));
    }
  }

  public static int sumDigits(int n)
  {
    int sum = 0;
    while(n > 0)
    {
      int digit = n % 10;
      sum += digit * digit;
      n /= 10;
    }
    return sum;
  }

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy