943,832 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1528
  • C++ RSS
Apr 27th, 2009
0

Largest Prime Factors

Expand Post »
I need some advice from people who know what they're are talking about. The question I am struggling with is below and my attempt is below that. Can you tell where I'm going wrong? Thanks.

I'm trying to acheive this:

(A) Read the first name and the surname of a person.
Calculate and output n1 = the integer representing the first letter (counting
from the left) of the first name in the ASCII table.
For example, if the first name is “James”, then n1=74 (i.e. the value representing ‘J’ is
74 in the ASCII table).
Note that there is no restriction that the first letter entered must be a capital letter.
(B) Calculate and display n2 = the integer representing the first letter of the
surname in the ASCII table.
For example, if the surname is “Bond”, then n2 = 66 (i.e. the value representing ‘B’ is
66 in the ASCII table).
(C) Calculate and display n3 = the squared digit length of n1.
The following process determines the squared digit length of an integer. Take any
integer and add up the squares of its digits. This will give you another integer. Repeat
this procedure until the number you end up with is 1 or 4. The number of times this
process has to be repeated before it gets to 1 or 4 is the squared digit length. For
example, if we start with 85, we get:
82 + 52 = 89
82 + 92 = 145
12 + 42 + 52 = 42
42 + 22 = 20
22 + 02 = 4
This process shows that the squared digit length of 85 is 5.
According to the experts, this process will always eventually reach either 1 or 4. The
squared digit length of 1 and 4 is zero, since we don't actually have to apply the
process to them to reach the stopping condition (interestingly, though, if we did apply
the process, to 1, we would keep getting 1, but if we applied it to 4, we would get a
repeating sequence: 4, 16, 37, 58, 89, 145, 42, 20, 4).
(D) Calculate and display n4 = the squared digit length of n2.
(E) Calculate and display n5 = the largest prime factor of n3+n4.
A prime factor of integer n is a factor of n which is a prime number. A prime number
is any integer greater than 1 and only divisible by itself and 1 (e.g. 2, 3, 5, 7, 11, 13,
17 etc). For example, 3 is the largest prime factor of 27 and 7 is the largest prime
factor of 49.

And this is what I have so far:

#include <iostream>
#include <cmath>

using namespace std;


bool largest_prime_factor(int b);
int squared_digit_length(int a);


int main()
{
char ch1, ch2;
int n1, n2, n3, n4, n5; // used for the 5 steps of the task

cout << "Enter your first name and surname: " << flush;
cin >> ch1; // read the first letter of the first name
cin.ignore(100, ' '); // ignore the rest characters in the first name
cin >> ch2; // read the first letter of the surname

n1 = (int)ch1;
n2 = (int)ch2;
n3 = squared_digit_length(n1);
n4 = squared_digit_length(n2);
n5 = n4 + n5;

cout << n1 << n2 << endl;
squared_digit_length(n1);
squared_digit_length(n2);
largest_prime_factor(n5);

return 0;
}

int squared_digit_length(int a)

{
int result = 0;
while (a)
{
int t = a % 10;
result += t * t;
a /= 10;
}
return result;
}




bool largest_prime_factor(int a)
{
int main()
{

int n3,n4,n5;
cout << "n3=" ;
cin >> n3 ;
cout << "n4= " ;
cin >> n4;
n5 = n3+n4;
cout << largest_prime_factor(n5) << endl;
return 0;
}

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SallyJ is offline Offline
7 posts
since Apr 2009
Apr 27th, 2009
0

Re: Largest Prime Factors

Wow, that post was a nightmare to read!
Can you use code tags in future please?!

The maths in this looked terrible at first glance too...How does 82 + 52 = 89??
It took me a while to work out that you meant, but I got there in the end!
85 becomes (8*8) + (5*5) = 89! Now I see!

Anyways, I've posted some comments in your code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // shouldn't this be returning an int??
  7. bool largest_prime_factor(int b);
  8.  
  9. int squared_digit_length(int a);
  10.  
  11. // This bit looks ok at a brief glance....
  12. int main()
  13. {
  14. char ch1, ch2;
  15. int n1, n2, n3, n4, n5; // used for the 5 steps of the task
  16.  
  17. cout << "Enter your first name and surname: " << flush;
  18. cin >> ch1; // read the first letter of the first name
  19. cin.ignore(100, ' '); // ignore the rest characters in the first name
  20. cin >> ch2; // read the first letter of the surname
  21.  
  22. n1 = (int)ch1;
  23. n2 = (int)ch2;
  24. n3 = squared_digit_length(n1);
  25. n4 = squared_digit_length(n2);
  26. n5 = n4 + n5;
  27.  
  28. // until we get here...
  29.  
  30. // would it be worth adding "n1 = " and " n2= " to the cout below?
  31. cout << n1 << n2 << endl;
  32. // do you also need to cout the squared digit lengths of n1 and n2? (values of n3 and n4)
  33.  
  34. // Why are you calling squared_digit_length on n1 and n2 here?
  35. // you already have the relevant values stored in n3 and n4. Also you aren't even assigning the returned values to an identifier here!
  36. squared_digit_length(n1);
  37. squared_digit_length(n2);
  38.  
  39. // you should also ensure you set up an int variable here to store the value returned by this function call
  40. largest_prime_factor(n5);
  41.  
  42. return 0;
  43. }
  44.  
  45. // This bit looks ok, you're passing in a value, perfoming a calculation and returning the result...
  46. // But the algorithm you're using won't quite do what you want it to do! see your description between steps C and D
  47. int squared_digit_length(int a)
  48. {
  49. int result = 0;
  50. while (a)
  51. {
  52. int t = a % 10;
  53. result += t * t;
  54. a /= 10;
  55. }
  56. return result;
  57. }
  58.  
  59.  
  60. // But What is this block of code about??
  61. // This function doesn't do anything practical...
  62. // You aren't using the value that gets passed into the function for anything, your return type doesn't match the declared return type (bool)
  63. // This function looks like it should return an int value.
  64. // Plus the function doesn't actually do anything other than ask the user to input values for n3 and n4 before calling itself in the cout.
  65. // Calling this function once will cause the function to recursively call itself in the final cout.
  66. // also in the declaration of the function (at the top of the file) doesn't quite match the definition below:
  67. bool largest_prime_factor(int a)
  68. {
  69. //int main() - This bit shouldn't be in here!
  70. //{
  71. int n3,n4,n5;
  72. cout << "n3=" ;
  73. cin >> n3 ;
  74. cout << "n4= " ;
  75. cin >> n4;
  76. n5 = n3+n4;
  77. cout << largest_prime_factor(n5) << endl;
  78. return 0; // should return true or false with bool return type..
  79. // however, this function is supposed to be calculating and returning the largest prime factor of the passsed in number....
  80. // so you should change the return type of the function to int and return an int!
  81. //}
  82. }

I recommend that you strip out the contents of the largest_prime_factor function and look up an algorithm for the desired functionality..
so for now:
C++ Syntax (Toggle Plain Text)
  1. int largest_prime_factor(int a)
  2. {
  3. // todo: insert code for largest prime factor algorithm here
  4. //(return 0 for now until new code is added)
  5. return 0;
  6. }

Also, as mentioned in the code comments your squared_digit_length function does not do what it's supposed to be doing. It should do something more like this:

C++ Syntax (Toggle Plain Text)
  1. int squared_digit_length(int a)
  2. {
  3. // val is a number we'll be recursively manipulating
  4. // initially assign it the passed in value (a).
  5. // result will store the result of the additions of the squared digits
  6. // digit_length will count the number of iterations before the result is equal to 1 or 4
  7. int val = a, result=0, digit_length=0;
  8.  
  9. // loop until result is 1 or 4
  10. while(result!=1 && result!=4)
  11. {
  12. // split value of val into hundreds, tens and units
  13. int hundreds = val / 100;
  14. int remainder = val % 100;
  15. int tens = remainder / 10;
  16. int units = remainder % 10;
  17.  
  18. // calculate result by adding the squares of the hundreds, tens and units....
  19. result = (hundreds*hundreds) + (tens*tens) + (units*units);
  20. val = result; // update val with the value of result
  21. digit_length++; // increment digit_length.
  22. }
  23. // squared digit length is the number of repeats of the process, so return digit_length...Not result!
  24. return digit_length;
  25. }

So I hope that's at least part of your problem solved.
I'll leave you to try to work out some kind of algorithm for the largest_prime_factor calculation!

Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Apr 27th, 2009
0

Re: Largest Prime Factors

i have quickly read it and i think this is what you want
C++ Syntax (Toggle Plain Text)
  1. #include <conio>
  2. #inlcude <iostream>
  3. void main()
  4. {
  5. char a[50];
  6. int b,c,r,s,t,i=0,h,z;
  7. ///////////////////////////////////// a part
  8. cout<<"enter a name";
  9. cin>>a;
  10. b=int(a[0]);
  11. cout<<"the ascii value of first letter of first name is"
  12. <<b;
  13. //////////////////////////////////// b part
  14. while(a[i]!=' ') //assuming that sir name is after a space
  15. {
  16. i++;
  17. }
  18. i++;
  19. c=int(a[i]);
  20. cout<<"the ascii value of first letter of sir name is"
  21. <<c;
  22. ///////////////////////////////////// c part
  23. z=b;
  24. r=0;s=0;t=0;
  25. i=0;
  26. while(1)
  27. {
  28. while(1)
  29. {
  30. if(z>=100)
  31. {r=(z%100)*(z%100);
  32. z/=100;}
  33. else if(z<100)
  34. {s=(z%10)*(z%10);
  35. z/=10}
  36. else if(z<10)
  37. {t=z*z;
  38. break;}
  39. }
  40. z=r+s+t;
  41. r=0;s=0;t=0;
  42. i++;
  43. if((z==1)||(z==4))
  44. {break;}
  45. }
  46. cout<<"squared digit length of the ascii value of first letter of first name is"
  47. <<i;
  48. ///////////////////////////////////// d part
  49. z=c;
  50. r=0;s=0;t=0;
  51. h=0;
  52. while(1)
  53. {
  54. while(1)
  55. {
  56. if(z>=100)
  57. {r=(z%100)*(z%100);
  58. z/=100;}
  59. else if(z<100)
  60. {s=(z%10)*(z%10);
  61. z/=10}
  62. else if(z<10)
  63. {t=z*z;
  64. break;}
  65. }
  66. z=r+s+t;
  67. r=0;s=0;t=0;
  68. h++;
  69. if((z==1)||(z==4))
  70. {break;}
  71. }
  72. cout<<"squared digit length of the ascii value of first letter of sir name is"
  73. <<h;
  74. //////////////////////////////////// e part
  75. i=i+h;
  76. h=2;
  77. while(i>1)
  78. {
  79. if(i%h<0)
  80. {h++;}
  81. else
  82. {i/=h;}
  83. }
  84. cout<<"the largest prime factor in this case is"
  85. <<h;
  86. }
Last edited by threat; Apr 27th, 2009 at 9:57 am.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
threat is offline Offline
10 posts
since Apr 2009
Apr 27th, 2009
0

Re: Largest Prime Factors

My previous post was a little messy...so apologies for that....

Below is a tidied up version of your code....This is how I would've tackled the problem...

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int largest_prime_factor(int const &a);
  8. int squared_digit_length(int const &a);
  9.  
  10. int main()
  11. {
  12. // store forename and surname in std::strings...Safer than using char arrays!
  13. string forename, surname;
  14. int n1,n2,n3,n4,n5;
  15. cout << "Enter your forename and surname : ";
  16. cin >> forename >> surname;
  17.  
  18. // TODO: do you need to perform any range checking on the users input?
  19.  
  20. // assuming no range checking required....just use the int values of the first characters stored in
  21. // the strings
  22. n1 = (int)forename[0];
  23. n2 = (int)surname[0];
  24. cout << endl << "Integer value for 1st letter of forename (" << forename[0] << ") = " << n1 << endl;
  25. cout << "Integer value for 1st letter of surname (" << surname[0] << ") = " << n2 << endl;
  26.  
  27. // calculate and output the digit lengths...
  28. n3 = squared_digit_length(n1);
  29. n4 = squared_digit_length(n2);
  30. cout << "squared digit length of " << n1 << " is " << n3 << endl;
  31. cout << "squared digit length of " << n2 << " is " << n4 << endl;
  32.  
  33. // calculate and output the largest prime factor.
  34. n5=largest_prime_factor(n3+n4);
  35. cout << "The largest prime factor is: " << n5 << endl;
  36.  
  37. return 0;
  38. }
  39.  
  40. // Calculate squared digit length of passed-in value.
  41. // NOTE: passing a constant reference to an int...Probably overkill for a simple example like this!
  42. // Using constant references ensures that the passed in value remains constant and is not altered in any way!
  43. // It's just good practice!
  44. // If anything in the function attempts to modify the passed in value, the compiler will complain!
  45. int squared_digit_length(int const &a)
  46. {
  47. // val is the number we'll be recursively manipulating
  48. // initially assign it the passed in value (a).
  49. // result will store the result of the additions of the squared digits
  50. // digit_length will count the number of iterations before the result is 1 or 4
  51. int val = a, result=0, digit_length=0;
  52.  
  53. // loop until result is 1 or 4
  54. while(result!=1 && result!=4)
  55. {
  56. // split value of val into hundreds, tens and units
  57. int hundreds = val / 100;
  58. int remainder = val % 100;
  59. int tens = remainder / 10;
  60. int units = remainder % 10;
  61.  
  62. // calculate result by adding the squares of hundreds, tens and units....
  63. result = (hundreds*hundreds) + (tens*tens) + (units*units);
  64. val = result; // set val to the value of result
  65. digit_length++; // increment digit_length.
  66. }
  67.  
  68. // squared digit length is the number of repeats of the process, so return digit_length...
  69. return digit_length;
  70. }
  71.  
  72. // calculate largest prime factor of passed-in int value
  73. int largest_prime_factor(int const &a)
  74. {
  75. //TODO: You'll need to find an appropriate algorithm to code for this section...
  76. // I'm not doing all of your homework for you! ;)
  77. // Perhaps you could try decyphering Threats cryptic post!
  78. return 0;
  79. }

As you can see I've left a couple of blanks for you to fill....Have fun!

Cheers for now,
Jas.
Last edited by JasonHippy; Apr 27th, 2009 at 11:24 am. Reason: typo...
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Apr 27th, 2009
0

Re: Largest Prime Factors

threat do you think you could write out that program using her variables instead to make it easier to read?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SD123 is offline Offline
1 posts
since Apr 2009
Apr 27th, 2009
0

Re: Largest Prime Factors

okay, i've refined my code, my first code is wrong, i made it in a hurry, the code below is simple, perfect and complete and uses your variable names
C++ Syntax (Toggle Plain Text)
  1. #include <conio>
  2. #include <iostream>
  3. #include <stdio>
  4. #include <math>
  5. //NOTE: if code gives error, add .h to the name of header files
  6. //i've checked this code works perfect in borland c++
  7. void main()
  8. {
  9. char a[50];
  10. int n1,n2,n3=0,n4=0,n5=2,i=0,r,s,t,z;
  11. ///////////////////////////////////// a part
  12. cout<<"enter a name\n";
  13. gets(a); //to input a character array containing spaces we use gets (header file: stdio)
  14. n1=int(a[0]); //converts a character into its ascii code
  15. cout<<"the ascii value of first letter of first name is\n"
  16. <<n1;
  17. //////////////////////////////////// b part
  18. while(a[i]!=' ') //assuming that sir name is after a space
  19. {
  20. i++;
  21. }
  22. i++;
  23. n2=int(a[i]);
  24. cout<<"\nthe ascii value of first letter of sir name is\n"
  25. <<n2;
  26. ///////////////////////////////////// c part
  27. z=n1;
  28. while((z!=1)&&(z!=4))
  29. {
  30. r=pow(z%10,2); //using power function for taking square (header file: math)
  31. z/=10;
  32. s=pow(z%10,2);
  33. z/=10;
  34. t=pow(z%10,2);
  35. z=r+s+t;
  36. n3++;
  37. }
  38. cout<<"\nthe squared digit length of n1 is\n"
  39. <<n3;
  40. ///////////////////////////////////// d part
  41. z=n2;
  42. while((z!=1)&&(z!=4))
  43. {
  44. r=pow(z%10,2);
  45. z/=10;
  46. s=pow(z%10,2);
  47. z/=10;
  48. t=pow(z%10,2);
  49. z=r+s+t;
  50. n4++;
  51. }
  52. cout<<"\nthe squared digit length of n1 is\n"
  53. <<n4;
  54. //////////////////////////////////// e part
  55. z=n3+n4;
  56. while(z>1)
  57. {
  58. if(z%n5==0) //when remainder is zero, it means n5 is one of the prime factor
  59. {z/=n5;}
  60. else
  61. {n5++;} //if n5 is not prime factor, then we move on to next number
  62. }
  63. cout<<"\nthe largest prime factor of n3+n4 is\n"
  64. <<n5; //the last factor in prime factorization is always the largest
  65. getch();
  66. }
Reputation Points: 7
Solved Threads: 0
Newbie Poster
threat is offline Offline
10 posts
since Apr 2009
Apr 28th, 2009
-1

Re: Largest Prime Factors

ok, thats funny, just correct a typo in my last post at line 52:
C++ Syntax (Toggle Plain Text)
  1. cout<<"\nthe squared digit length of n1 is\n"
it is actually
C++ Syntax (Toggle Plain Text)
  1. cout<<"\nthe squared digit length of n2 is\n"
Reputation Points: 7
Solved Threads: 0
Newbie Poster
threat is offline Offline
10 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linker error help?
Next Thread in C++ Forum Timeline: About pointers to data members





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC