Nov 25, 2009

The Necklace Problem

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

The Necklace Problem

demolaynyc
Hey, I need help! Can someone code this problem in C++

The instructions are:

An interesting problem in number theory is sometimes called the "necklace problem." This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones-digit. This process is repeated until the "necklace" closes by returning to the original two numbers. For example, if the starting numbers are 1 and 8, twelve steps are required to close the "necklace":

1 8 9 7 6 3 9 2 1 3 4 7 1 8

Here's another program:

It says to...

modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits

Here's the code:

CODE

/* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */

#include <iostream.h>

int main() {

    long numValue; //user input
    cout <<"Enter an integer: ";
      cin >>numValue;

    long MaxDigits = 100000000;
    
    while (MaxDigits > numValue) { //while loop to get how many digits numValue has
        MaxDigits /= 10;
    }

    long sum = 0;    //sum of all digits
    while (MaxDigits >=1) {
        long digit = numValue / MaxDigits; //get digit
        sum += digit*digit*digit;
        numValue = numValue % MaxDigits; //enter new value for numValue
        MaxDigits /= 10;       //ex: 100 => 10
    }
    
    

    cout <<"Sum of the cubes of the digits is " <<sum <<endl;
    return(0);
}

 

 

 


Comment/Reply (w/o sign-up)

pyost
Since I have never worked in C++, forgive me if there are any errors, since I'm going through a tutorial in order to solve this.

CODE

#include <iostream>

int main ()
{
  // Variables
  short first;
  short second;
  short previous;
  short current;
  short help;

  // Input and communication with the user
  cout << "Insert the first digit: ";
  cin >> first;
  cout << "\n";
  cout << "Insert the second digit: ";
  cin >> second;
  cout << "\n";
  cout << "The necklace is: ";

  // Preparing for the loop
  previous = second;
  current = (first + second)%10;
  cout >> first;
  cout >> second;

  // And not the WHILE loop

  while ( (previous != first) || (current != second) )
    {
      cout >> current;
      help = previous;
      previous = current;
      current = (help + current)%10;
    }

  // Writing the last digit
  cout >> current;

  return 0;
}


So, what are we doing here? First, the user inputs two digits. Next, we create what the second and the third digit would be. After that, it's all in the loop. We create the next number if we haven't reached the first two. When we have, the program won't enter the loop, and will write the last remaining digit (which equals the second digit).

 

 

 


Comment/Reply (w/o sign-up)

demolaynyc
wow. no experience in c++ yet you have the answer. such skill. Thanks a lot man, ill go try that out.

Comment/Reply (w/o sign-up)

pyost
It's not all about knowing the language, but knowing how to solve the problem wink.gif That's what I've been doing in my school for the past year. We are using Pascal, but doesn't stop us from solving extremely hard problems biggrin.gif The necklace one is easy laugh.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Necklace


    Looking for necklace, problem

See Also,

*SIMILAR VIDEOS*
Searching Video's for necklace, problem
advertisement



The Necklace Problem

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com