How can I input this C++ code in Dart [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to make this C++ code in Dart, but I find it really difficult. I tryed watching Darts video and searching on the web, but with no success.Could someone be able to give me a hand?
This is the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n < 0) return 1;
int sum = 0;
int i = 0;
while (i <= n) sum += i*i;
cout << sum;
return 0;
}

something like
library x;
import 'dart:io';
void main(List<String> args) {
int n;
print('input a number');
String input = stdin.readLineSync();
n = int.parse(input);
print('n: $n');
if(n < 0) {
exit(1);
}
int sum = 0;
int i = 0;
while(i <= n) {
print(sum);
sum += i * i;
}
print(sum);
}
But don't expect to much.
When reaching the while loop sum and i are 0.
This way you have produced a nice endless loop to busy your computer ;-)

You could do the calculation bit (sum of squares of all numbers from 1 to n inclusive) with a recursive function like:
int recur(int n) => (n > 0) ? (n * n) + recur(n - 1) : 0;
Then it's a simple matter of figuring out how to enter n and output recur(n). That can be done with stdin.readLineSync and print. That would be along the following lines:
int recur(int n) => (n > 0) ? (n * n) + recur(n - 1) : 0;
void main( List<String> args ) {
int inNum;
String input = stdin.readLineSync();
inNum = int.parse( input );
if (inNum < 0) {
exit( 1 );
}
print( recur( sum ) );
}
Just be careful with large input values, I'm not sure whether Dart is smart enough to do tail end recursion optimisation. If not, stack space may be an issue.

Related

I can't find the error in my code to find the largest prime factor? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've written a code to find the largest prime factor that has, so far, worked for every single case that i've tested for, yet it fails when I input 600851475143. It keeps giving me 5102831 which is incorrect. I'm not sure why this happens even though I've checked it, help would be appreciated.
#include <iostream>
long int get_largest_prime_factor(long int);
int main()
{
std::cout << get_largest_prime_factor(600851475143);
return 0;
}
long int get_largest_prime_factor(long int prime_Number)
{
for(long int r = prime_Number - 1; r != 1; r--)
{
if(prime_Number % r == 0)
{
long int a = get_largest_prime_factor(r);
long int b = get_largest_prime_factor(prime_Number / r);
if(a == b)
return a;
return a > b? a : b;
}
}
return prime_Number;
}
Use this easy algorithm to get the right prime factor:
long long int getMaxPrimeFactor(long long int n) {
int i, max = -1;
while (n % 2 == 0) {
max = 2;
n /= 2;
}
for (i = 3; i <= sqrt(n); i = i + 2)
while (n % i == 0) {
max = i;
n /= i;
}
max = (n > 2) ? n : max;
return max;
}
This should output you:
6857

Too many recursive calls or too many cout? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Prints specific sequence by given n. For example n=1: 1, n=2: 121, n=3,1213121, n=4: 121312141213121 and so on. But for n > 11 the program stops working after 3556 cout. I think the problem is too many cout or too many recursive calls, but im not sure how to fix it or even is that really the problem. Please let me know if u have any solutions. Thank you so much!
#include <iostream>
using namespace std;
int findNumberByIndex(int index, int& number, int n) {
if (index < 0) {
return 0;
}
if (index % 2 == 0) {
return 1;
}
if (index == ((int)pow(2, number - 1) - 1)) {
return number;
}
index -= (int)pow(2, number);
findNumberByIndex(index, number, n);
}
int printSeq(int rowLen, int& index, int& number, int n, int& counter) {
if (index == rowLen) {
return 0;
}
int result = findNumberByIndex(index, number, n);
if (result == 0) {
number++;
}
else {
cout << result;
index++;
number = 2;
}
printSeq(rowLen, index, number, n, ++counter);
}
int main()
{
int n, index = 0, number = 2, seqLen = 1;
cin >> n;
int counter = 0;
if (n < 0 || n >= 20) {
return 0;
}
int rowLen = ((int)pow(2, n - 1) + (int)pow(2, n - 1) - 1);
printSeq(rowLen, index, number, n, counter);
cout << endl;
return 0;
}
In the definition of findNumberByIndex, you forgot to
return findNumberByIndex(index, number, n);
In the definition of printSeq, you forgot to
return printSeq(rowLen, index, number, n, ++counter);
This leads to undefined behaviour.
Regarding your hypothesis
I think the problem is too many cout or too many recursive calls
Unless specific concerns you can revoke the idea of "too many output". You could suffer from a too deep recursive call tree, but in your specific example your functions are tail-recusrive. With optimization correctly turned on, any decent compiler should optimize them.

My c++ programm in Visual Studio closes without reading FOR function; [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Can someone tell me why this programm gives only the cout text?
#include "iostream"
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
float x;
int y[50];
int n;
cout << "Dati notele despartindule prin Enter";
for ( n = 0; n == 5; n++) {
cin >> y[n];
if (n >= 1) {
y[n] = y[n - 1] + y[n];
}
}
x = y[n] / (n + 1);
cout << x;
return 0;
}
for ( n = 0; n == 5; n++)
Is not what you want. A for-loop is nothing but a while-loop using different syntax:
n = 0;
while(n == 5)
{
// you loop body here
n++;
}
As you can see, it executes while your condition is true. In this case... not at all because it's not true to begin with.
You probably meant
for ( n = 0; n < 5; n++)
n == 5 is the problem. When came to for function and makes the test to see if the block needs to run it get false because you initialize with n = 0; better than n == 5 use n!=5 but if you skip to make n = 5 you get an infinite loop.
For the best case use n < 5.

Printing 1 to n using Fibonacci recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to print Fibonacci series from 1 to n in my function.
I know that I can do it by writing a regular Fibonacci and using it in a for block to print 1 to N. Like this:
#include <iostream>
using namespace std;
int fibo(int);
int main(){
for (int i = 0; i < 5; i++)
cout << fibo(5);
system("pause");
return 0;
}
int fibo(int n){
if (n == 1 || n == 2)
return 1;
else
return fibo(n - 1) + fibo(n - 2);
}
but my problme is that I can't do it without for,IN my function
I mean I want to Print it with a recursive algorithm
Here is my code up to now
#include <iostream>
using namespace std;
int fibo(int, bool);
int main(){
fibo(5, false);
system("pause");
return 0;
}
int fibo(int n, bool IsPrinted){
if (n == 1 || n == 2){
if (!IsPrinted)
cout << 1 << endl;
return 1;
}
else{
int temp = fibo(n - 1, IsPrinted) + fibo(n - 2, IsPrinted);
if (!IsPrinted){
cout << temp << endl;
IsPrinted = true;
}
return temp;
}
}
long fibo(int N, bool print) {
long value = 0;
if(1 == N)
value = 1;
if(1 < N)
value = fibo(N-1, print) + fibo(N-2, false);
if(print)
std::cout << N << " => " << value << std::endl;
return value;
}
int main(){
fibo(5, true);
return 0;
}
What you should realize is that the calls to the fibo function makes a tree. The root of the tree is the call to fibo(5, true) in the main(). As you only want to print each value once, the solution is to decide to print the value of the function only on the leftmost branch of that tree. The rule is then simply:
never print when on a right branch (hence the call to fibo(N-2, false)
never print if the parent didn't print (to avoid printing when on a child left branch of a right branch)
A common solution is to use memoization:
int fibo(int n)
{
static std::map<int,int> memo;
auto it=memo.find(n);
if(it!=std::end(memo))
{
return it->second;
}
int ret=1;
if (n > 2)
{
ret = fibo(n - 1) + fibo(n - 2);
}
memo[n]=ret;
return ret;
}
Then you can safely loop over the input parameters without recomputing the values over and over again:
for(int i=0;i<20;++i)
{
std::cout<<i<<" "<<fibo(i)<<std::endl;
}
Note that this is not only advantageous in printing but also for the calculation itself (at least as long as you call the function more than once).
Beside the above, you should also consider using long or double for the return type, as int will overflow more quickly.
EDIT: Ok, after your edit I don't know whether my answer exactly fits to your question, but I think it's a good advice anyway.
But here is another quick alternative which comes close, I guess:
int fibo(int n, bool first=true)
{
int ret=0;
if(n>2)
{
ret=fibo(n-1,false)+fibo(n-2,false);
}
else
{
ret=1;
}
if(first)
{
std::cout<<ret<<std::endl;
}
return ret;
}
DEMO

How to make this algorithm faster? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to come up with the following algorithm:
The input is unsigned integer number.
The output is the size of the array of unordered pairs of unsigned integers, which, when multiplied, give a number less then or equal to the input.
I have one naive implementation working, but it is way too slow for my purpose (compl. O(n^2), please correct me if I am wrong). My question is: how to make it faster?
#include <iostream>
using namespace std;
bool notInYet(int t[][1], int mi, int ma, int m) {
bool val = true;
for(int i = 0; i < m; i++)
if(t[i][0] == mi && t[i][1] == ma)
val = false;
return val;
}
int main() {
int n, m;
int t[100000][1];
cin >> n;
m = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j*i <= n && j <= i; j++) {
if(notInYet(t, j, i, m)) {
t[m][0] = j;
t[m][1] = i;
//cout << "t[" << m << "] = (" << t[m][0] << ", " << t[m][1] << ")" << endl;
m++;
}
}
}
cout << m << endl;
return 0;
}
I think it should be something like that - pseudocode:
int counter = 0;
for int i = 1 to sqrt(input), i++ {
if (input % i == 0) counter++;
}
counter is an answer if you need unique pairs, otherwise you need to multiply it by 2 (and sub 1 if input % sqrt(input) == 0)
If I'm reading correctly #jauser's algorithm doesn't get what you want.
If the target is 5, then the pairs are (1,1)(1,2)(1,3)(1,4)(1,5)(2,2). So the answer is 6. His algorithm will produce 1 because 5 mod 1 == 0, but not mod 2.
In general, if the target is n, then you know (1,k) is a counted pair for all k from 1 to n. There are n - 1 + 1 = n of these. Now you have (2,k) for k from 2 to floor(n/2) (skip 1 because your pairs are unordered). There are n/2-2+1 of these. Continue this through (j,k) for j= floor(sqrt(n)). Putting this is pseudocode
count = 0;
for j in 1 .. floor(sqrt(n))
count += floor(n / j) - j + 1;
Maybe there is even some clever series solution that gets this to a constant time calculation.
Am I missing something in the problem?
Well, you are spending a lot of time effectively calculating the following per i:
j= n/i;
So if you just do that you reduce the complexity to O(n). You can halve it also since the list will contain both (i, j) and (j, i) when i!=j, but that won't reduce the overall complexity.