Jump to content
xisto Community
Sign in to follow this  
demolaynyc

The Necklace Problem

Recommended Posts

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:

/* 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);}

Share this post


Link to post
Share on other sites

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.

#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).

Share this post


Link to post
Share on other sites

It's not all about knowing the language, but knowing how to solve the problem :P 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 :P The necklace one is easy :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.