The Necklace Problem

free web hosting
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);
}

 

 

 


Reply

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

 

 

 


Reply

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

Reply

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

Reply


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*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. the necklace opinion - 61.85 hr back. (1)
  2. c sum digits() - 148.41 hr back. (1)
  3. the necklace (conflict) - 261.89 hr back. (1)
Similar Topics

Keywords : necklace, problem

  1. Mysql Problem
    Linking tables (1)
  2. Problem With Acdsee
    is default application (4)
    A girlfriend has a problem with ACDSee (for Pentax). For some reason it has been set as the default
    application for opening folders, so everytime a folder is clicked, ACDSee opens. If already tried to
    fix the file association (changing the default action, adding a new action that opens explorer, but
    it won't work /huh.gif" style="vertical-align:middle" emoid=":huh:" border="0" alt="huh.gif"
    /> ). I've also tried to untick the "browse with acdsee" option in ACDSee, but it doesn't
    remember this option. How can I fix this ?....
  3. I Can't Turn Off My Computer
    problem with turning of computer... (9)
    today I downloaded several files, and after this I can't delete one of them , and the main
    problem is that I can't turn off my computer... why ?? does anyone know this? please help me
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />
    P.S sorry for my bad English /sad.gif" style="vertical-align:middle" emoid=":(" border="0"
    alt="sad.gif" />....
  4. Problem With Move_uploaded_file()
    Permission denied? (5)
    CODE Warning: move_uploaded_file(images/profile/Mordent.jpg)
    [function.move-uploaded-file]: failed to open stream: Permission denied in
    /home/mordent/public_html/withvalour/edit_image.php on line 42 Warning:
    move_uploaded_file() [function.move-uploaded-file]: Unable to move
    '/tmp/phpOWVz7o' to 'images/profile/Mordent.jpg' in
    /home/mordent/public_html/withvalour/edit_image.php on line 42 Any ideas? The code seems fine,
    as it works a treat when I run it locally (on WAMPServer), but as soon as I tr....
  5. Live Messenger Display Pic Problem
    (5)
    I'm having a problem with my display picture on Live Messenger. Everytime I log off and then
    log back on, it changes back to one I don't use anymore (what's in my avatar). What I
    normally have is this (LOL) but it changes back to the LOL one all the time. It's really
    annoying. It also does it to my brother, he has some funny gnome on it and it changes back to that
    duck that comes with it. So does anyone know why it does it and how to fix it? ....
  6. Administrator Account Problem In Microsoft Xp [solved]
    I have lost "my documents" (20)
    I have problem with administrator account ... I created new administrator account with this way:
    start /control panel /account users/new account and than I created a new one ; after this I turned
    off computer . I wanted to have two skypes turned on and my friend suggested me to do so... when I
    turned it on the second day , I coudn't enter with the first administrator ,and I entered with
    the administrator that I had created . I have lost my document folder.. please help me . how can I
    enter or can I enter with that first administrator account?? Can my documents be ....
  7. Problem With Purchase Credits Page
    Problem with Purchase Credits Page (7)
    I was trying to checkout the Purchase Credits section, today, and found a dead end to 404.
    http://www.astahost.com/purchase_credits.htm I was using the link from the menu above ->
    Credits\Purchase Credits Need to buy some credit, I'm really busy lately with my work, not
    much time to keep track of post. Btw, I have a question here too. I seems that if I'm inactive
    for a while, like not posting for a few days, the email alert for new post will not come anymore. Is
    it a feature or a bug?....
  8. A Gaiaonline Problem
    A problem i want to stop (9)
    I have a problem getting on Gaiaonline. When I go it always says "SLOW DOWN Slow down,
    hotshot! You've been loading pages too fast, and our system doesn't like it. Take a
    break for a few minutes so we can catch up! This is not a ban. Come back in a littlw while,
    ok?" and I want it to stop! So can anyone please help me out here?....
  9. Trojan / Virus Problem ,please Help
    might be (hoon) (18)
    I have been infected with atrojan but i can detect it. and i have detected Hoon trojan and deleted
    it ,but the symptoms of the trojan is still on my pcs network " all driver have an autoplay (right
    click by mouse) and it gives my this message by d-click on any driver ************** SYS.EXE
    QUOTE windows cannot find 'sys.exe',make sure you typed the name correctly and then try
    again.to search for a file ,clicl the start botton then clicl search the virus was detected and
    deleted from all drivers: C:\sys.exe D:\sys.exe E:\sys.exe F:\sys....
  10. Fedora Core 6 Install Problem
    (6)
    I have tried three times to get Fedora Core 6 to install but I keep getting the same error message:
    “there was an error running your transaction, for the following reason(s): file conflicts”. I am
    using all of the default settings except changing the time zone and basically installing all
    packages. The error always happens near the end of the fifth disk. I have run sha hash checking on
    all media and everything checks out. Has anyone had this problem or a possible solution?....
  11. Internet Explorer 7 Problem
    Works fine in Firefox! (Shocker) (8)
    Hey, I'm helping my mom with one of her web projects for class and I've noticed a pretty
    big problem (no thanks to IE7) that cost her a few points. It won't load an image of an email
    icon, however, Firefox will load and display it properly. Here's the line for the image: CODE
    <img src="nationalparks/images/email.gif" width="99" height="100"
    border="0" alt="Email" /> Also, IE7 complains about some ActiveX content,
    but this is just a basic page and NO JavaScript, NO Java applets, NO ActiveX of any ki....
  12. Permission Problem With Mysql Database Creation
    Please Help! (8)
    I seem to have a problem with accessing my database with proper permissions. I have set the my
    database correctly giving my db username all priviliges yet i seem to be unable to even log on with
    this username with a denied access error. Any ideas on resolving this?....
  13. Win Rar Password Problem
    (7)
    i have some trouble with some winrar files password the files is for an ISO and iam trying to remove
    the password because some files has a different password i have tried to change the password but no
    luck ...i used set default password and no luck too if any1 have any idea please post it thanks in
    advance -joe....
  14. Problem With Drag And Drop (or So It Seems).
    (11)
    I'm running on Windows XP with Service Pack 1. The last few days, all of a sudden, the drag and
    drop functionality of explorer (or anything on the operating system that uses system calls in order
    to perform it) screws up completely. I attempt to drag it, and it just flickers a bit to the
    direciton I'm moving it and then fails. It doesn't seem to be the mouse as I can still drag
    a rectangle selection to select multiple files etc, just no drag and drop. However, it isn't
    always like that, it works generally (as far as I know) and stop working out of now....
  15. Frustrating Problem With XP On Laptop
    (19)
    I have my Dualcore AMD Acer laptop, 5102Wlmi, with a 120GB hd, 2 GB ram, etc. I had a friend buy it
    in the states and bring it over just 2 months ago. All of a sudden, the other day when i went to
    turn it on, it would bring up the xp boot screen, and then just hang there. I tried a different hard
    drive. no luck. I tired installing XP, but after booting from cd and loading all the drivers, right
    after the FAT and NTFS drivers, it says "Setup is loading Windows" and then it hangs there. I've
    left it overnight and in the morning it's still in the same place. I can&#....
  16. Spam Problem On My Forums
    (25)
    I’m starting to have a real problem in my forums. Apparently “midget anal porn” is the hottest new
    thing. I didn’t think midgets were into that kind of thing but apparently I’m wrong (apologies to
    any midgets out there). I knew that spam may be a problem on my board but I didn’t think that I
    would be a target just yet. I only have 16 registered users (including the spam bots). How in the
    world did they find me? Just like junk mail I figure this problem is only going to get worst since
    I am now apparently on the list. Does anyone have a solution to this problem? I....
  17. Blue Screen - irql_not_less_or_equal
    computer OS Windows problem booting (29)
    Hi , My computer AMD64 256mb RAM / 80GB HDD / Windows XP Professional recently started showing a
    blue screen while starting .. QUOTE A problem has been detected and windows has been shut down
    to prevent further damage to your computer . IRQL_NOT_LESS_OR_EQUAL (blah blah blah ... )
    Technical Information: *** STOP: 0x0000000A (oxFB07D354, ox000000FF, ox00000001, ox804E2E41)
    Initially, if I restarted it would not come the next time. It used to come only randomly. But
    later it started coming more often, and now it did not start for quite a few times con....
  18. Problem With The ReadOnly Property
    (3)
    I have a problem setting the readonly property of a textbox which depends on a yes and no radio
    buttons selection, I'm sure that my code is correct but i dont know why nothing happens. This is
    code: CODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML> <HEAD> <TITLE> New Document </TITLE> <script
    LANGUAGE="JavaScript"> <!-- Begin function changeRadio(sVal) {     if
    (sVal=='n')     {         a.textval.readonly=true;         a.texval.value="&#....
  19. Laptop Overheating
    Problem with Dell 600m (15)
    One of my friends has a Dell 600m which he bought around the same time as me. We pretty much have
    the exact same specs, but my processor and video memory is a little better. He has been having
    problems since the time he got the laptop with it overheating to the point of it locking down.
    I'll agree that the Dell 600m's from the Summer of 2003 will get way too hot for normal
    laptop use. My laptop has only locked up once or twice for me because of it overheating, but his
    computer will lock up every time he doesn't have it on a cooling pad. And even then the....
  20. Need To Hack An Admin Account On Xp... No Problem!
    It's so easy to hack an account you'll be amazed (61)
    Well recently one of my good girl that is a friend got a laptop from her dad. Her dad does websites
    so the laptop was new and worked fine, but needed to be defraged. The one problem, her nor her dad
    knew the admin password. I told her to post her question on Trap 17 and it got answered with in
    minutes. All you have to do is these few steps: 1. Reboot 2. Before the windows logo comes up press
    F11 (Just start clicking it over and over again until the windows logo comes up.) 3. Just sit and
    let it do it's thing and when the login screen comes up click on the Admin icon....
  21. Photoshop Cropping Problem
    (6)
    All right. For most of my Photoshop work, I use Photoshop 6.0. Yes, I know it's old, but it
    gets the job done, not to mention I just don't have the money of Photoshop CS or Photoshop CS2
    right now. But I'm getting off topic. Anyways, a few months ago, when I went to crop a
    picture, it wouldn't crop all the way. It would crop to a certain point and then just stop and
    refused to go any further with the cropping box. It seems to only form squares and sometimes
    horizontal rectangles, but only to a certain extent. However, when I opened Adobe Photosho....
  22. XP Problem: Clicking On Folder Opens Search
    (4)
    I have no idea why, but for some reason whenever I click on a folder now it opens it as a search
    instead of just showing me the contents. Whenever I right click it, search is at the top of the tab
    in bold instead of open. Is there any way I can fix this? I have no idea what I did wrong. Sorry,
    I fixxed it.....
  23. Einstein Quiz
    Can you solve this problem? (29)
    Hi everybody; I want to ask the quiz written by Einstein last century.It is called Einstein's
    Riddle.Einstein said that 98% of the people in the world cannot solve his riddle. Are you among the
    other 2%?(Hint:It is really easy:) Hints: 1: There are 5 houses in 5 different colors 2: In each
    house lives a person with a different nationality. 3: These 5 owners drink a certain beverage, smoke
    a certain brand of cigar and keep a certain pet. 4: No owners have the same pet, smoke the same
    brand of cigar or drink the same drink. Further Details: a: The Brit lives in a re....
  24. Problem Deleting Empty Folder In Win XP
    why you can not delete an empty folder? (13)
    It happened to me several times. When a folder is already empty, but still can't delete it.
    Which says something about other user is using it. Yet I am the only user on my PC. Anyone knows why
    is it? And how to deal with it?....
  25. Problem: Network Connection Fails Every 2 Mins
    (12)
    From time to time, my internet connection falls out; It's been happening since i installed XP
    again. In the beginning it was 2 minutes after you started up the pc. Happily, now it's jsut
    when you download a big file or so. Does anyone know how to solve my problem?? Thanks, Michiel....
  26. DVD-RW Problem: Unformatted Disk
    (6)
    Recently I buyed a couple of DVD-rw to make a backup of my files since my father is planning to
    reïnstall the whole computer. But when I try to burn some files it says: "Unformatted disk" (I'm
    not sure if I translated it well from Dutch, but I'm quite sure this means the same). Does
    anyone know how to solve this? I knew you had to format floppy disks before using them but I
    didn't know that you also had to do that with DVD's. thnx in advance, -=jeroen=-....
  27. Problem: I Need Urgent Help. Can't Boot.
    (9)
    Hi all. Thanks for reading this, and helping me... /unsure.gif' border='0'
    style='vertical-align:middle' alt='unsure.gif' /> I have a big problem - I must fix one computer
    for one of my important clients, but, everything seems to be fine, until you turn it on (really
    /rolleyes.gif' border='0' style='vertical-align:middle' alt='rolleyes.gif' /> ) ehm, anyway, when
    computer is turned ON, it boots and when it comes to login screen just blocks, and freezes, and
    there's nothing to do there. Sometimes I can manage to click on user to login, but then freezes
    again. .....
  28. Mp3 Player Problem: Sound In One Ear Only
    after dropping down on ground (8)
    Okay... Hi everyone... Umm...my MP3 Player has recently had/developed a new problem. The thing is,
    im a very,very, VERY clumsy person. I always drop things including my MP3 Player and my phone.
    Before the last time i dropped the MP3 Player, it was working fine. But ever since the accident, it
    has stopped giving me sound from one ear of my headphones. I am sure that my Headphones are alright
    as they work fine on other systems like my PC and my Walkman and CD Player. But..i need to know how
    to fix this problem. Often, the sound comes out of the other ear if i twist and tur....
  29. Mozilla Firefox And Runescape
    Problem I had with FFox and RScape. (10)
    When I tried to play Runescape in Mozilla's Free browser (Firefox), I couldn't. However,
    when I tried to play the game in Microsoft Internet Explorer, I could. It may be possible to play
    Runescape in Firefox by altering Firefox's settings, or it could just have been me or chance or
    something that caused me to have a problem.....
  30. Error 406 - Problem In My Phpbb Forum
    Actually a problem with Apache (8)
    When I post the message in my forum contant the word "system" it can't work and said the HTTP
    error 406. I add three Mod : attach mod 2.3.11 , cash mod 2.2.2 , pay money mod 1.0.7 phpBB
    verison 2.0.12 my forum http://siuwing.astahost.com/testmod and then I also setup a model forum
    by cPanel , the error of posting also occur -> http://www.siuwing.astahost.com/testmod2/ the
    model forum havent adding any mod what is the problem ...? /sad.gif' border='0'
    style='vertical-align:middle' alt='sad.gif' /> ....

    1. Looking for necklace, problem

Searching Video's for necklace, problem
Similar
Mysql
Problem -
Linking
tables
Problem With
Acdsee - is
default
application
I Can't
Turn Off My
Computer -
problem with
turning of
computer...
Problem With
Move_uploade
d_file() -
Permission
denied?
Live
Messenger
Display Pic
Problem
Administrato
r Account
Problem In
Microsoft Xp
[solved]
- I have
lost "my
documents
4;
Problem With
Purchase
Credits Page
- Problem
with
Purchase
Credits Page
A Gaiaonline
Problem - A
problem i
want to stop
Trojan /
Virus
Problem
,please Help
- might be
(hoon)
Fedora Core
6 Install
Problem
Internet
Explorer 7
Problem -
Works fine
in
Firefox!
(Shocker)
Permission
Problem With
Mysql
Database
Creation -
Please
Help!
Win Rar
Password
Problem
Problem With
Drag And
Drop (or So
It Seems).
Frustrating
Problem With
XP On Laptop
Spam Problem
On My Forums
Blue Screen
-
irql_not_les
s_or_equal -
computer OS
Windows
problem
booting
Problem With
The ReadOnly
Property
Laptop
Overheating
- Problem
with Dell
600m
Need To Hack
An Admin
Account On
Xp... No
Problem!
- It's
so easy to
hack an
account
you'll
be amazed
Photoshop
Cropping
Problem
XP Problem:
Clicking On
Folder Opens
Search
Einstein
Quiz - Can
you solve
this
problem?
Problem
Deleting
Empty Folder
In Win XP -
why you can
not delete
an empty
folder?
Problem:
Network
Connection
Fails Every
2 Mins
DVD-RW
Problem:
Unformatted
Disk
Problem: I
Need Urgent
Help.
Can't
Boot.
Mp3 Player
Problem:
Sound In One
Ear Only -
after
dropping
down on
ground
Mozilla
Firefox And
Runescape -
Problem I
had with
FFox and
RScape.
Error 406 -
Problem In
My Phpbb
Forum -
Actually a
problem with
Apache
advertisement




The Necklace Problem



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE