The nth term of the sequence of triangle numbers is given by, tn = 1/2 n (n+1);
ID: 3567438 • Letter: T
Question
The nth term of the sequence of triangle numbers is given by, tn = 1/2 n (n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55,...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
NB: this means A = 1!!
Step 1: Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? You will need to load the file and read each word. Use the ifstream class.
Step 2: Save all the triangle words that you found to a single file. You will need to use the ofstream class.
Explanation / Answer
Int main()
{
//Assume input is on the desktop
String filename=”Words.txt”;
BruteForce(filename);
}
public void BruteForce(string filename) {
int result = 0;
string[] words = readInput(filename);
for(int i = 0; i < words.Length; i++){
double wordsum = (Math.Sqrt(1+8*sum(words[i])) - 1.0) / 2.0;
if (wordsum == ((int)wordsum)) {
result++;
}
}
Cout<<"There are {0} triangle words", result<<result;
}
string[] readInput(string filename) {
ifstream(filename);
string line = r.ReadToEnd();
r.Close();
string[] names = line.Split(',');
for (int i = 0; i < names.Length; i++) {
names[i] = names[i].Trim('"');
}
return names;
}
private int sum(string name) {
int result = 0;
for (int i = 0; i < name.Length; i++) {
result += Convert.ToInt32(name[i]) - 64;
}
return result;
}
}
}
// TriangleWords.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include <cmath>
using namespace std;
int getCharValue(char a);
double sum1(string name);
int main()
{
int a[500];
int n;
int t=0;
string value[500];
string words;
string word[500];
int count=0;
ifstream fread;
ofstream fwrite;
cout<<endl;
int i=0;
char c[50];
fread.open ("Words.txt");
fwrite.open("TriangleWords.txt");
if (!fread.is_open())
cout<<"Unable to open a file."<<endl;
if(!fwrite.is_open())
cout<<"Unable to open a file."<<endl;
while( fread.good() )
{
getline ( fread, words, ' ');
if (words!="")
{
word[i]=words;
cout<<words<<endl;
double wordsum = (sqrt(1+8*sum1(word[i])) - 1.0) / 2.0;
if(wordsum==(int)wordsum)
{
fwrite<<words;
fwrite<<" ";
count++;
}
i++;
}
}
cout<<"Total triangle words are: "<<count++<<endl;
fread.close();
fwrite.close();
system("pause");
return 0;
}
double sum1(string name)
{
int result = 0;
for (int i = 0; i < name.length(); i++)
{
result +=(int)name[i] - 64;
}
return result;
}
Sample text is:
Import Scanner class to accept the input from the user by passing the System.in object.
Import IOException class to throw unhandled exceptions.
Create a class called PositiveAndNegativeNumbers.
Declare the required variables that are to be used in the program.
Create an array to accept the array of integers.
Uses “for” loop to check the number of positive and negative integers are present in the array.
Condition to check. If the value present in the array is greater than or equal to zero, then the element is said to positive integer, or else negative integer.
Sample output is:
by
are
check
are
than
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.